How to create custom category attribute and category custom tab in magento 2. - Magesan

How to create custom category attribute and category custom tab in magento 2.

How to create custom category attribute and category custom tab in magento 2.

Custom attributes are something we need to create in our day to day life for adding additional functionality.
category attributes are limited and if we need additional functions then we need to create custom category attribute.
Here, we create simple_content wysiwyg field using InstallData.php
app/code/Magesan/Extension/Setup/InstallData.php

<?php
namespace Magesan\Extension\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Catalog\Model\Category;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {        
        $setup->startSetup();
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            Category::ENTITY, 'simple_content', [
                'group'  => 'content',
                'label'  => 'Simple Content',
                'type'   => 'text',
                'input'  => 'textarea',
                'global' => ScopedAttributeInterface::SCOPE_STORE,
                'sort_order' => 333,
                'source'     => '',
                'visible'    => true,
                'required'   => false,
                'user_defined' => false,
                'default' => null,
                'wysiwyg_enabled' => true,
                'is_html_allowed_on_front' => true,
                'backend' => ''
            ]
        );
        $setup->endSetup();
     }
 }

Save the file and fire command
php bin/magento setup:upgrade
php bin/magento cache:clean

After creating attribute. We need to call that attribute in category_form.xml file
app/code/Magesan/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="custom_section" sortOrder="40">
        <settings>
            <collapsible>true</collapsible>
            <label translate="true">Custom Section</label>
        </settings>
        <container name="custom_tab" sortOrder="180">
            <field name="simple_content" template="ui/form/field" sortOrder="10" formElement="wysiwyg">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="wysiwygConfigData" xsi:type="array">
                            <item name="settings" xsi:type="array">
                                <item name="theme_advanced_buttons1" xsi:type="string">bold,italic,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,link,unlink,image,|,bullist,numlist,|,code</item>
                                <item name="theme_advanced_buttons2" xsi:type="boolean">false</item>
                                <item name="theme_advanced_buttons3" xsi:type="boolean">false</item>
                                <item name="theme_advanced_buttons4" xsi:type="boolean">false</item>
                                <item name="theme_advanced_statusbar_location" xsi:type="boolean">false</item>
                            </item>
                            <item name="height" xsi:type="string">10px</item>
                            <item name="toggle_button" xsi:type="boolean">false</item>
                            <item name="add_variables" xsi:type="boolean">true</item>
                            <item name="add_widgets" xsi:type="boolean">false</item>
                            <item name="add_images" xsi:type="boolean">true</item>
                            <item name="add_directives" xsi:type="boolean">true</item>
                        </item>
                        <item name="source" xsi:type="string">category</item>
                    </item>
                </argument>
                <settings>
                    <scopeLabel>[STORE VIEW]</scopeLabel>
                    <label translate="true">Content</label>
                    <dataScope>simple_content</dataScope>
                </settings>
                <formElements>
                    <wysiwyg class="Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg">
                        <settings>
                            <rows>4</rows>
                            <wysiwyg>true</wysiwyg>
                        </settings>
                    </wysiwyg>
                </formElements>
            </field>
        </container>
    </fieldset>
</form>

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>