? No Code Attached Yet
avatar brianteeman
brianteeman
24 Dec 2021

Finally getting my head around the web asset stuff.

Love that I can add one line to a component to add multiple scripts using the presets.

What I am missing (maybe it exists and I'm not skilled enough to see it) is the ability to use joomla language strings in the same way.

For example I have a preset that contains multiple scripts and stylesheets and it needs to use one language string. This currently means that I have to add that string to the component php as well ;(

Example

// Attach all items from foobar preset to the document
$wa->usePreset('foobar');
// Grab any language strings that are needed in the preset
Text::script('JGLOBAL_COLUMNS');

Not too bad with one string but a real pia with multiple. It would be great to list them in the preset or similar

avatar brianteeman brianteeman - open - 24 Dec 2021
avatar joomla-cms-bot joomla-cms-bot - change - 24 Dec 2021
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 24 Dec 2021
avatar Fedik
Fedik - comment - 24 Dec 2021

That would be a good feature, especially for complex clientside apps (like media manager).
Not sure how it should be implemented, maybe something like: Text::scriptLanguageFile('blabla.script.ini') which load whole file to Text::script().

At current state Developer can implement own WebAsset item class for the script, and load constants from there.
The class should implement WebAssetAttachBehaviorInterface https://docs.joomla.org/J4.x:Web_Assets#Advanced:_Custom_WebAssetItem_class
With onAttachCallback method, that will call Text::script for all constants, not much nice but should work.

Something like this:

class MyFancyFoobarAssetItem extends WebAssetItem implements WebAssetAttachBehaviorInterface
{
  public function onAttachCallback(Document $doc): void
  {
    Text::script('BLABLA_CONSTANT');
    Text::script('BLABLA_CONSTANT2');
    Text::script('BLABLA_CONSTANT3');
    ...
  }
}
avatar Fedik Fedik - change - 24 Dec 2021
Labels Added: ?
avatar Fedik Fedik - labeled - 24 Dec 2021
avatar wilsonge
wilsonge - comment - 6 Jan 2022

Actual example here for the form validation https://github.com/joomla/joomla-cms/blob/4.0-dev/libraries/src/WebAsset/AssetItem/FormValidateAssetItem.php with it's web asset definition

{
"name": "form.validate",
"type": "script",
"class": "FormValidateAssetItem",
"attributes": {
"defer": true
},
"dependencies": [
"core"
],
"uri": "system/fields/validate.min.js"
},

But as @Fedik says this is the intended & documented solution for now. Happy if something more concrete as a proposal comes up to considering it - but closing this for now as it's definitely possible with the current code.

avatar wilsonge wilsonge - change - 6 Jan 2022
Status New Closed
Closed_Date 0000-00-00 00:00:00 2022-01-06 00:05:18
Closed_By wilsonge
avatar wilsonge wilsonge - close - 6 Jan 2022
avatar brianteeman
brianteeman - comment - 6 Jan 2022

thanks

Add a Comment

Login with GitHub to post a comment