0

I am using Magento 2.3.2. I have created custom dropdown attribute for category. All is good until I enter admin category page. Spinner keeps spinning and in console there is error "TypeError: value.map is not a function" (file-uploader.js:79:27). There is something with mapping, but I can't figure out what is wrong. Did all from examples, but nothing helps. Attribute is loaded and values in dropdown also loaded. I can remove spinner from html and I can select from dropdown, even save. Issue is that spinner not removed and previously saved value is not preselected. Any ideas?

category_form.xml:

<fieldset name="display_settings">
    <field name="category_icon">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Module\Vendor\Model\Category\Attribute\Source\Icons</item>
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">1</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 Icon</item>
            </item>
        </argument>
    </field>
</fieldset>

Icons.php

class Icons extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource{
protected $fontAwesome;

public function __construct(
    \Awps\FontAwesome $fontAwesome
)
{
    $this->fontAwesome = $fontAwesome;
}

/**
 * @return array
 */
public function getAllOptions()
{
    if (!$this->_options) {
        $fonts = $this->fontAwesome->getAllData();
        $options = [];
        foreach ($fonts as $font)
        {
             $options[] = ['value' => $font['class'], 'label' => $font['name']];
        }
        $this->_options = $options;
    }
    return $this->_options;
}

public function getOptionText($value)
{
    foreach ($this->getAllOptions() as $option) {
        if ($option['value'] == $value) {
            return $option['label'];
        }
    }
    return false;
}

InstallData.php

class InstallData implements InstallDataInterface{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'category_icon', [
            'type'     => 'varchar',
            'label'    => 'Category Icon',
            'input'    => 'select',
            'source'   => 'Module\Vendor\Model\Category\Attribute\Source\Icons',
            'visible'  => true,
            'required' => false,
            'global'   => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'group'    => 'Display Settings',
        ]);
    }}
0

Please check this.

Step 1 : create InstallData.php file : Company\Module\Setup\InstallData.php Now In this file, create an Array of the attributes that you want to add as below.

$attributeGroup = 'Test Group';
        $attributes = [
            'attribute_code' => [
                'group'              => $attributeGroup,
                'input'              => 'select',
                'type'               => 'int',
                'label'              => 'Attribute Label',
                'visible'            => true,
                'required'           => false,
                'user_defined'               => true,
                'searchable'                 => false,
                'filterable'                 => false,
                'comparable'                 => false,
                'visible_on_front'           => false,
                'visible_in_advanced_search' => false,
                'is_html_allowed_on_front'   => false,
                'used_for_promo_rules'       => true,
                'source'                     => 'Company\Module\Model\Config\Source\Options',
                'frontend_class'             => '',
                'global'                     =>  \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'unique'                     => false,
                'apply_to'                   => 'simple,grouped,configurable,downloadable,virtual,bundle'
            ],...
          ];

Step 2 : Now Add and Assign Attribute to Group and Attribute Set.

// Add Attribute
// Magento\Eav\Setup\EavSetupFactory  $this->eavSetupFactory
// Magento\Framework\Setup\ModuleDataSetupInterface $setup

 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            foreach ($attributes as $attribute_code => $attributeOptions) {
                $eavSetup->addAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                    $attribute_code,
                    $attributeOptions
                );
            }
//Assign 
            foreach ($attributes as $attribute_code => $attributeOptions) {

                // Class \Magento\Eav\Model\AttributeManagement

                $this->attributeManagement->assign(
                    'catalog_product',
                    $attributeSet->getId(),
                    $groupId,
                    $attribute_code,
                    $attributeSet->getCollection()->getSize() * 10
                );
            }

Note :

$attributeSet : you can load the attribute collection and loop for it or Can pass specific id instead. $groupId : Magento\Eav\Model\Entity\Attribute\Group : you can get the group id with the help of this class.

Now , Options for drop down attribute , you can add options directly as a array. and another approach is to use class Magento\Eav\Model\Entity\Attribute\Source\AbstractSource as follows :

<?php

namespace Company\Module\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
    * Get all options
    *
    * @return array
    */
    public function getAllOptions()
    {
        $this->_options = [
                ['label' => __('No'), 'value'=>'0'],
                ['label' => __('Yes'), 'value'=>'1'],
                ['label' => __('Other'), 'value'=>'2']
            ];

    return $this->_options;

    }

}

Please read each line carefully, so that you can create the attributes easily.

  • You show example of product attribute. I need to do it to category. They bit different, seems so – G Strato Sep 29 at 7:02

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.