0

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.

    1 Answer 1

    0

    Turns out I can modify the config array with a name item just as I have done for the other attribute values.

    <field> <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="name" xsi:type="string">MyVendor\MyModule\Helper\Data::MEPS_SCALE_ATTR_ID</item> <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 name="label" xsi:type="string">MyVendor\MyModule\Helper\Data::MEPS_SCALE_ATTR_LABEL</item> </item> </argument> </field> 

    It seems that the next challenge is getting the label to get the value of the PHP const rather than outputting the string as a literal.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.