2

:InstallData

<?php
namespace Vender\Extension\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\Category\Attribute\Backend\Sortby as SortbyBackendModel;
use Magento\Catalog\Model\Category\Attribute\Source\Sortby;

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,
            'choosesubcategoryslider',
            [
                'group' => 'Choose Sub Category',
                'label' => 'Choose Sub Category Slider',
                'type'  => 'int',
                'input' => 'multiselect',
                'backend' => 'Vender\Extension\Model\Category\Attribute\Backend\Choosesubcategoryslider',
                'source' => 'Vender\Extension\Model\Config\Source\Choosesubcategoryslider',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'required' => false,
                'sort_order' => 20
            ]
        );
    }
}

backend:Vender\Extension\Model\Category\Attribute\Backend

<?php


namespace Vender\Extension\Model\Category\Attribute\Backend;

class Choosesubcategoryslider extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
    public function beforeSave($object)
    {
        $attributeCode = $this->getAttribute()->getName();
        if ($attributeCode == 'choosesubcategoryslider') {
            $data = $object->getData($attributeCode);
            if (!is_array($data)) {
                $data = [];
            }
            $object->setData($attributeCode, implode(',', $data) ?: null);
        }
        if (!$object->hasData($attributeCode)) {
            $object->setData($attributeCode, null);
        }
        return $this;
    }

    public function afterLoad($object)
    {
        $attributeCode = $this->getAttribute()->getName();
        if ($attributeCode == 'choosesubcategoryslider') {
            $data = $object->getData($attributeCode);
            if ($data) {
                if (!is_array($data)) {
                    $object->setData($attributeCode, explode(',', $data));
                } else {
                    $object->setData($attributeCode, $data);
                }
            }
        }
        return $this;
    }
}

sorce:Vender\Extension\Model\Config\Source

<?php
namespace Vender\Extension\Model\Config\Source;

class Choosesubcategoryslider extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * Catalog config
     *
     * @var \Magento\Catalog\Model\Config
     */
    protected $_catalogConfig;

    /**
     * Construct
     *
     * @param \Magento\Catalog\Model\Config $catalogConfig
     */
    public function __construct(\Magento\Catalog\Model\Config $catalogConfig)
    {
        $this->_catalogConfig = $catalogConfig;
    }

    /**
     * Retrieve Catalog Config Singleton
     *
     * @return \Magento\Catalog\Model\Config
     */
    protected function _getCatalogConfig()
    {
        return $this->_catalogConfig;
    }

    /**
     * {@inheritdoc}
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['label' => __('Label1'), 'value' => 'value1'],
                ['label' => __('Label2'), 'value' => 'value2'],
                ['label' => __('Label3'), 'value' => 'value3'],
                ['label' => __('Label4'), 'value' => 'value4']

            ];

        }
        return $this->_options;
    }
}

category_form.xml path: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="choossubcategory">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Choose Sub Category</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">80</item>
            </item>
        </argument>
        <field name="choosesubcategoryslider">
            <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">multiselect</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="default" xsi:type="string">0</item>
                    <item name="label" xsi:type="string" translate="true">Choose Sub Category Slider</item>
                </item>
            </argument>
        </field>
</form>

enter image description here

1 Answer 1

1

Magento 2 programmatically Category Multiselect Attributes Create

Step -1 Create File App\Code\Vendor\Extension\Setup\InstallData.php

<?php
namespace Vendor\Extension\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\Category\Attribute\Backend\Sortby as SortbyBackendModel;
use Magento\Catalog\Model\Category\Attribute\Source\Sortby;

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,
            'choosesubcategoryslider',
            [
                'group' => 'Choose Sub Category',
                'label' => 'Choose Sub Category Slider',
                'type'  => 'text',
                'input' => 'multiselect',
                'backend' => 'Vendor\Extension\Model\Category\Attribute\Backend\Choosesubcategoryslider',
                'source' => 'Vendor\Extension\Model\Config\Source\Choosesubcategoryslider',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'required' => false,
                'sort_order' => 20
            ]
        );
    }
}

Step -2 Create File App\code\Vendor\Extension\Model\Category\Attribute\Backend\Choosesubcategoryslider.php

<?php

namespace Vendor\Extension\Model\Category\Attribute\Backend;

class Choosesubcategoryslider extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
    public function beforeSave($object)
    {
        $attributeCode = $this->getAttribute()->getName();
        if ($attributeCode == 'choosesubcategoryslider') {
            $data = $object->getData($attributeCode);
            if (!is_array($data)) {
                $data = [];
            }
            $object->setData($attributeCode, implode(',', $data) ?: null);
        }
        if (!$object->hasData($attributeCode)) {
            $object->setData($attributeCode, null);
        }
        return $this;
    }

    public function afterLoad($object)
    {
        $attributeCode = $this->getAttribute()->getName();
        if ($attributeCode == 'choosesubcategoryslider') {
            $data = $object->getData($attributeCode);
            if ($data) {
                if (!is_array($data)) {
                    $object->setData($attributeCode, explode(',', $data));
                } else {
                    $object->setData($attributeCode, $data);
                }
            }
        }
        return $this;
    }
}

Step -3 create file App\code\Vendor\Extension\Model\Config\Source\Choosesubcategoryslider.php

<?php
namespace Vendor\Extension\Model\Config\Source;

class Choosesubcategoryslider extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * Catalog config
     *
     * @var \Magento\Catalog\Model\Config
     */
    protected $_catalogConfig;

    /**
     * Construct
     *
     * @param \Magento\Catalog\Model\Config $catalogConfig
     */
    public function __construct(\Magento\Catalog\Model\Config $catalogConfig)
    {
        $this->_catalogConfig = $catalogConfig;
    }

    /**
     * Retrieve Catalog Config Singleton
     *
     * @return \Magento\Catalog\Model\Config
     */
    protected function _getCatalogConfig()
    {
        return $this->_catalogConfig;
    }

    /**
     * {@inheritdoc}
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['label' => __('Label1'), 'value' => 'value1'],
                ['label' => __('Label2'), 'value' => 'value2'],
                ['label' => __('Label3'), 'value' => 'value3'],
                ['label' => __('Label4'), 'value' => 'value4']

            ];

        }
        return $this->_options;
    }
}

Step-4 Create file app\code\Vendor\Extension\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="choossubcategory">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Choose Sub Category</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">80</item>
            </item>
        </argument>
        <field name="choosesubcategoryslider">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Vendor\Extension\Model\Config\Source\Choosesubcategoryslider</item>
                <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">multiselect</item>
                    <item name="label" xsi:type="string" translate="true">Choose Sub Category Slider1</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

I hope this is helpful to you!! don't forget to Votes

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.