: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>