why cant use joomla.asset.json in my module?

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Locked
User avatar
mastalnise
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Thu Aug 12, 2021 6:33 am

why cant use joomla.asset.json in my module?

Post by mastalnise » Sun Nov 20, 2022 5:46 pm

Hi
I have developed a simple module and tried to use joomla.asset.json file in this module to register assets. The problem is that the assets written in this file are not registered and I have to use the PHP code to register the use of the assets.
I have used this method many times in Joomla and it has worked. But this is not the case in the Joomla module.

joomla.asset.json content:

Code: Select all

{
    "$schema": "https://developer.joomla.org/schemas/json-schema/web_assets.json",
    "name": "mod_instaads",
    "version": "4.2.3",
    "description": "This file contains details of the assets used by module instaads.",
    "license": "GPL-2.0-or-later",
    "assets": [
        
        {
            "name": "instaads",
            "type": "style",
            "uri": "mod_instaads/instaads.css"
        },
        {
            "name": "fontawesom",
            "type": "style",
            "uri": "system/joomla-fontawesome.min.css"
        },
        {
            "name": "all-instaads",
            "type": "preset",
            "description": "description",
            "dependencies": [
                "instaads#style",
                "fontawesom#style"
            ]
        }
        
    ]
}

This code does not work in the PHP section:

Code: Select all

use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->usePreset('all-instaads');
And I have to use this code:

Code: Select all

use Joomla\CMS\Factory;
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->registerAndUseStyle('instaads', 'mod_instaads/instaads.css');
In this code, there is no need to use joomla.asset.json file, but I insist on using this file for the asset register.
thanks for your help.

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: why cant use joomla.asset.json in my module?

Post by SharkyKZ » Sun Nov 20, 2022 8:21 pm

The asset JSON file is not loaded automatically for modules. You can load it like this:

Code: Select all

$wa->getRegistry()->addExtensionRegistryFile('mod_instaads');

User avatar
mastalnise
Joomla! Apprentice
Joomla! Apprentice
Posts: 26
Joined: Thu Aug 12, 2021 6:33 am

Re: why cant use joomla.asset.json in my module?

Post by mastalnise » Fri Nov 25, 2022 9:00 am

Hi
This code did not work.
The problem is that the asset is not registered in the Joomla.asset.json file.
Whether with this code or without this code

Code: Select all

$wa->getRegistry()->addExtensionRegistryFile('mod_instaads');
I have to register and use the asset with the following code:

Code: Select all

$wa->registerAndUseStyle('instaads', 'mod_instaads/instaads.css');
According to the Joomla asset documentation, when the name of an asset appears in the Joomla.asset.json file, it should be registered by itself and we only use it with this code:

Code: Select all

$wa->useStyle('instaads');

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2909
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: why cant use joomla.asset.json in my module?

Post by SharkyKZ » Fri Nov 25, 2022 9:07 am

Where is your joomla.asset.json file located?


Locked

Return to “Joomla! 4.x Coding”