Nilesh Tighare
Nilesh Tighare

Reputation: 913

Magento2 - Getting Issue in display multiselect category attribute options in Admin

In am trying to create multiselect category attribute from Installer script. The attribute is created. But I am getting the issue in option values in Magento 2 - Manage Category page. It only display blank textarea.

I have created this using below installer script:

/** @var EavSetup $eavSetup */
    $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

    /**
     * Add attributes to the eav/attribute for Category
     */

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'class',
        [
            'backend'      => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'type'         => 'varchar',
            'label'        => 'Class',
            'group'        => 'General Information',
            'input'        => 'multiselect',
            'source'       => '',
            'global'       => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible'      => true,
            'required'     => false,
            'user_defined' => true,
            'sort_order'   => 100,
            'option'     => [
                'value' => [
                    'SET' => ['SET'],
                    'HE'  => ['HE'],
                    'HBR' => ['HBR'],
                ]
            ],
        ]
    );

In database, I can see that, attribute is created in eav tables and option and values also there.

For fixing this, I have added xml in my custom module which has below xml(..\view\adminhtml\ui_component\category_form.xml).

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <field name="class">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">multiselect</item>
                    <item name="sortOrder" xsi:type="number">70</item>
                    <item name="label" xsi:type="string" translate="true">Select Class</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

But still getting issue in option values in admin category page. Textarea comes with no options. I have tried to cache:flush, static-content:deploy, setup:upgrade, indexer:reindex but still having issue. I am using Magento2.1.1 CE. Is there anything do I need to do? enter image description here Thanks

Upvotes: 1

Views: 2256

Answers (2)

Cristiano Casciotti
Cristiano Casciotti

Reputation: 1026

You should fix the option key in your configuration array like shown below:

'option' => [
    'values' => ['SET', 'HE', 'HBR'],
],

The values key is used to add options, the value key instead is used to update options that already exists, so be careful with them.

For the full explanation, see the addAttributeOption method code in the Magento\Eav\Setup\EavSetup class.

Upvotes: 2

faizanbeg
faizanbeg

Reputation: 391

source is blank thats why it showing blank in multi select use   
 'source'        => 'modulename/modelname',

and in source file use 

public function getAllOptions()
    {
        if (is_null($this->_options)) {
            $this->_options = array(array(
                'label' => Mage::helper('catalog')->__('Label'),
                'value' => 'value'
            ));
          }
        return $this->_options;
    }

Upvotes: 0

Related Questions