Skip to main content

Magento 2 : Dynamic XML entity attribute value

I am setting up a module to run on our Magento 2.4.6-p5 multi-website instances.

The module requires that a dropdown category attribute be setup with a default value and n other options (1 initially).

I have this working as is but my question is on the XML and PHP class constants. I know I can use the class constants within the XML element node values and this is what I am doing for example in the field->argument->item[name="config"]->item[name="default"]field->argument->item[name="config"]->item[name="default"] here:

...
<field name="meps_scale">
   <argument name="data" xsi:type="array">
       <item name="options" xsi:type="object">MyVendor\MyModule\Model\Category\Attribute\Source\MepsScale</item>
       <item name="config" xsi:type="array">
             <item name="dataType" xsi:type="string">string</item>
             <item name="formElement" xsi:type="string">select</item>
             <item name="default" xsi:type="string">MyVendor\MyModule\Helper\Data::DEFAULT_MEPS_SCALE_VALUE</item>
       </item>
   </argument>
</field>
...

My question is, can I use a class constant within a node attribute? And if so, how?

The use case being that I would like to replace the field name:

<field name="meps_scale">

with the PHP class constant of the attribute ID. Such as this pseudo code:

<field name="MyVendor\MyModule\Helper\Data::MEPS_SCALE_ATTR_ID">

I have looked at some of the Magento core module category_form.xml files but it seems they use literal values in the majority of modules.

Dynamic XML entity attribute value

I am setting up a module to run on our Magento 2.4.6-p5 multi-website instances.

The module requires that a dropdown category attribute be setup with a default value and n other options (1 initially).

I have this working as is but my question is on the XML and PHP class constants. I know I can use the class constants within the XML element node values and this is what I am doing for example in the field->argument->item[name="config"]->item[name="default"] here:

...
<field name="meps_scale">
   <argument name="data" xsi:type="array">
       <item name="options" xsi:type="object">MyVendor\MyModule\Model\Category\Attribute\Source\MepsScale</item>
       <item name="config" xsi:type="array">
             <item name="dataType" xsi:type="string">string</item>
             <item name="formElement" xsi:type="string">select</item>
             <item name="default" xsi:type="string">MyVendor\MyModule\Helper\Data::DEFAULT_MEPS_SCALE_VALUE</item>
       </item>
   </argument>
</field>
...

My question is, can I use a class constant within a node attribute? And if so, how?

The use case being that I would like to replace the field name:

<field name="meps_scale">

with the PHP class constant of the attribute ID. Such as this pseudo code:

<field name="MyVendor\MyModule\Helper\Data::MEPS_SCALE_ATTR_ID">

I have looked at some of the Magento core module category_form.xml files but it seems they use literal values in the majority of modules.

Magento 2 : Dynamic XML entity attribute value

I am setting up a module to run on Magento 2.4.6-p5 multi-website instances.

The module requires that a dropdown category attribute be setup with a default value and n other options (1 initially).

I have this working as is but my question is on the XML and PHP class constants. I know I can use the class constants within the XML element node values and this is what I am doing for example in the field->argument->item[name="config"]->item[name="default"] here:


<field name="meps_scale">
   <argument name="data" xsi:type="array">
       <item name="options" xsi:type="object">MyVendor\MyModule\Model\Category\Attribute\Source\MepsScale</item>
       <item name="config" xsi:type="array">
             <item name="dataType" xsi:type="string">string</item>
             <item name="formElement" xsi:type="string">select</item>
             <item name="default" xsi:type="string">MyVendor\MyModule\Helper\Data::DEFAULT_MEPS_SCALE_VALUE</item>
       </item>
   </argument>
</field>
...

My question is, can I use a class constant within a node attribute? And if so, how?

The use case being that I would like to replace the field name:

<field name="meps_scale">

with the PHP class constant of the attribute ID. Such as this pseudo code:

<field name="MyVendor\MyModule\Helper\Data::MEPS_SCALE_ATTR_ID">

I have looked at some of the Magento core module category_form.xml files but it seems they use literal values in the majority of modules.

Source Link

Dynamic XML entity attribute value

I am setting up a module to run on our Magento 2.4.6-p5 multi-website instances.

The module requires that a dropdown category attribute be setup with a default value and n other options (1 initially).

I have this working as is but my question is on the XML and PHP class constants. I know I can use the class constants within the XML element node values and this is what I am doing for example in the field->argument->item[name="config"]->item[name="default"] here:

...
<field name="meps_scale">
   <argument name="data" xsi:type="array">
       <item name="options" xsi:type="object">MyVendor\MyModule\Model\Category\Attribute\Source\MepsScale</item>
       <item name="config" xsi:type="array">
             <item name="dataType" xsi:type="string">string</item>
             <item name="formElement" xsi:type="string">select</item>
             <item name="default" xsi:type="string">MyVendor\MyModule\Helper\Data::DEFAULT_MEPS_SCALE_VALUE</item>
       </item>
   </argument>
</field>
...

My question is, can I use a class constant within a node attribute? And if so, how?

The use case being that I would like to replace the field name:

<field name="meps_scale">

with the PHP class constant of the attribute ID. Such as this pseudo code:

<field name="MyVendor\MyModule\Helper\Data::MEPS_SCALE_ATTR_ID">

I have looked at some of the Magento core module category_form.xml files but it seems they use literal values in the majority of modules.