1

Now, I've got empty dropdown.

Here is my code (InstallData.php):

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'width',
        [
            'type' => 'int',
            'label' => 'Width',
            'input' => 'select',
            'source' => 'Magento\Catalog\Model\Category\Attribute\Source\Page',
            'required' => false,
            'sort_order' => 4,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'is_html_allowed_on_front' => true,
            'group' => 'General Information',
        ]
    );

    $setup->endSetup();
}

And category_form.xml:

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="display_settings">
        <field name="width">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Width</item>
                    <item name="options" xsi:type="array">
                        <item name="33" xsi:type="string">33</item>
                        <item name="50" xsi:type="string">50</item>
                    </item>
                    <item name="default" xsi:type="number">50</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

1 Answer 1

2

Add following line


<item name="options" xsi:type="object">Magento\Catalog\Model\Category\Attribute\Source\Page</item>

So your xml looks like


<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="display_settings">
        <field name="width">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Catalog\Model\Category\Attribute\Source\Page</item>
                <item name="config" xsi:type="array">
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Width</item>
                    <item name="options" xsi:type="array">
                        <item name="33" xsi:type="string">33</item>
                        <item name="50" xsi:type="string">50</item>
                    </item>
                    <item name="default" xsi:type="number">50</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Clear cache.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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