Magento2.1 Category custom attribute dropdown
-
30-09-2020 - |
Question
Steps to reproduce
1. Module UpgradeData.php script contains:
$categorySetup->addAttribute(Category::ENTITY, 'roflcopter', [
'type' => 'int',
'label' => 'CMS Block',
'input' => 'select',
'source' => 'Magento\Catalog\Model\Category\Attribute\Source\Page',
'required' => false,
'sort_order' => 20,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'Display Settings',
]);
2. 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="Navigation">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Navigation</item>
<item name="collapsible" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
<field name="roflcopter">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="sortOrder" xsi:type="number">60</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">Roflcopter</item>
</item>
</argument>
</field>
</fieldset>
</form>
Expected result
- In category form should appear dropdown select Roflcopter with CMS Blocks as options
Actual result
- Empty dropdown
Solution
Add options tag for creating select options. In your case this should be
<field name="roflcopter">
<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="sortOrder" xsi:type="number">70</item>
<item name="dataType" xsi:type="string">string</item>
<item name="formElement" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">Roflcopter</item>
</item>
</argument>
</field>
OTHER TIPS
I have done in my case. I have custom options ex. L1, L2 and L3. I need to get them on custom attribute as values. So I was created a source file in module - vendor\module\Model\Config\Source\Options.php
this file contain the small code to create the options, Here you can follow the code
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\module\Model\Config\Source;
/**
* Catalog category landing page attribute source
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* {@inheritdoc}
* @codeCoverageIgnore
*/
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = [
['value' => 'l1', 'label' => __('L1')],
['value' => 'l2', 'label' => __('L2')],
['value' => 'l3', 'label' => __('L3')],
];
}
return $this->_options;
}
/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
return [
'l1' => __('L1'),
'l2' => __('L2'),
'L3' => __('L3'),
];
}
}
then after in your installdata.php you have to call this as source
$eavSetup->addAttribute(
Category::ENTITY,
'category_level_rendering',
[
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Category Level rendering',
'input' => 'select',
'required' => false,
'sort_order' => 100,
'source' => '',
'visible' => true,
'source' => 'vendor\module\Model\Config\Source\Options',
'default' => '0',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'General Information',
'used_in_product_listing' => true,
]
);
Then also add the line in xml file
<field name="category_level_rendering">
<argument name="data" xsi:type="array">
/*Here is the code added to get the options on dropdown*/
<item name="options" xsi:type="object">Vendor\module\Model\Config\Source\Options</item>
<item name="config" xsi:type="array">
<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 name="label" xsi:type="string" translate="true">Category Level Rendering</item>
</item>
</argument>
</field>
Save it, flush cache and please check.
Hopefully it helps to you.
Please give me reply if it works to you.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange