No Code Attached Yet
avatar 1vanguard
1vanguard
2 Sep 2021

I am developing a view for the Joomla 4 component. I am trying to connect the style and script files through the WebAssetManager.

use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->registerScript('com_component.corejs', 'com_component/assets/js/component.js');

if ($wa->assetExists('script', 'com_component.corejs')) {
    var_dump('Script "com_component.corejs" exists!');
}

$wa->useScript('com_component.corejs');

$wa->registerStyle('com_component.corecss', 'com_component/assets/css/component.css', [], ['data-foo' => 'some attribute'], []);
$wa->useStyle('com_component.corecss');

The assetExists check is performed, but none of the files are included in the of the document. The json file (/media/com_component/joomla.asset.json) contains an asset with my script.

What could be the problem?

Sorry for my English

Votes

# of Users Experiencing Issue
1/1
Average Importance Score
5.00

avatar 1vanguard 1vanguard - open - 2 Sep 2021
avatar 1vanguard 1vanguard - change - 2 Sep 2021
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 2 Sep 2021
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 2 Sep 2021
avatar Fedik
Fedik - comment - 3 Sep 2021

can be wrong path 'com_component/assets/js/component.js'
if your js in 'media/com_component/js/component.js' then use 'com_component/component.js'

avatar dgrammatiko
dgrammatiko - comment - 3 Sep 2021

if your js in 'media/com_component/js/component.js' then use 'com_component/component.js'

Not if but static assets SHOULD be stored in the media folder

avatar 1vanguard
1vanguard - comment - 3 Sep 2021

can be wrong path 'com_component/assets/js/component.js'
if your js in 'media/com_component/js/component.js' then use 'com_component/component.js'

I misunderstood the Joomla4 docks. Files of any assets must be located along the path: media/com_component/..
Plus, having looked into the internal components, I corrected the connection. In the end, everything worked :)

$ wa = $ this-> document-> getWebAssetManager ();
$ wa-> getRegistry () -> addExtensionRegistryFile ('com_component');

$ wa-> registerStyle ('component_styles', 'com_component / component.css', [], [], []);
$ wa-> useStyle ('component_styles');

$ wa-> registerScript ('component_js', 'com_component / component.js');
$ wa-> useScript ('component_js');
avatar 1vanguard 1vanguard - change - 3 Sep 2021
Status New Closed
Closed_Date 0000-00-00 00:00:00 2021-09-03 14:10:40
Closed_By 1vanguard
avatar 1vanguard 1vanguard - close - 3 Sep 2021

Add a Comment

Login with GitHub to post a comment