0

I've something like this:

            $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'test_att',
            [
                'type' => 'varchar',
                'input' => 'select',
                'input' => 'text',
                'required' => false, 
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => 1,
                'unique' => false,
                'apply_to' => 'simple,configurable',
                'group' => 'test',
            ]
        );

I want to use an object to get the options from a dynamic method instead of a fixed array.

According to EavSetup addAttribute method it's not possible because the option parameter expects an array

    if (isset($attr['option']) && is_array($attr['option'])) {
        $option = $attr['option'];
        $option['attribute_id'] = $this->getAttributeId($entityTypeId, $code);
        $this->addAttributeOption($option);
    }

So I used product_form.xml

<fieldset name="test">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">test</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">10</item>
        </item>
    </argument>
    <field name="test_att">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Company\Module\Model\Source\Object</item>
            <item name="config" xsi:type="array">
                <item name="required" xsi:type="boolean">false</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">false</item>
                </item>
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">select</item>
            </item>
        </argument>
    </field>
</fieldset>

It works BUT two dropdowns are shown, one using the options from the object, one not (empty select). Also the customized dropdown has no scope selector or scope label.

Same solution works for custom category attributes (even if without scope)

How to do it ??? Probably I misunderstood something

0

In product_form.xml:

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="mobile">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">Mobile</item>
            <item name="provider" xsi:type="string">product</item>
            <item name="dataScope" xsi:type="string">data.product</item>
            <item name="sortOrder" xsi:type="number">150</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="opened" xsi:type="boolean">false</item>
            <item name="ns" xsi:type="string">product_form</item>
        </item>
    </argument>

    <field name="attribute_name">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">10</item>
                <item name="dataType" xsi:type="string">int</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="label" xsi:type="string" translate="true">Attribute Name</item>
                <item name="dataScope" xsi:type="string">attribute_name</item>
            </item>
        </argument>
    </field>

</fieldset>

After this, run "cache:flush". It works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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