0

I have a module in which I would like to add a stylesheet and a script. I am creating a lot of modules for this project, and I would like to use a centralized json file for my scripts and styles, since a lot of them are used here and there.

$wa = Factory::getDocument()->getWebAssetManager();
$wr = $wa->getRegistry();
$wr->addRegistryFile(JPATH_SITE.'/media/com_foo/joomla.asset.json');
$wa->useStyle('com_foo.foo');
$wa->useScript('com_foo.fooscript');

My current code throws the following:

There is no "com_foo.foo" asset of a "style" type in the registry.

Shouldn't this be possible? Am I doing it wrong?

1 Answer 1

1

You have to use a relative path in your call to addRegistryFile, ie:

$wr->addRegistryFile('media/com_foo/joomla.asset.json');
2
  • 1
    There is also the shorthand addExtensionRegistryFile() method where you can pass just the component name, i.e. $wr->addExtensionRegistryFile('com_foo');.
    – Sharky
    Oct 31, 2023 at 21:08
  • Thanks to both, that is great! Nov 1, 2023 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.