3

I want to add a new text area to my categories in Magento2, similar to this, but its for Magento 1: adding custom drop down field to category

Basically there should be a field for each category where my customer can put in some category-based SEO content. I want to echo it later on the sidebar.

How could I achieve this? Bonus points if I can achieve this with pure XML.

2 Answers 2

0

You need to create a module that has Setup/InstallData.php with content similar to

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
        $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
        $attributeSetId = $categorySetup->getDefaultAttributeSetId($entityTypeId);
        $categorySetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'bottom_description');
        $categorySetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY, 'bottom_description', [
                'type' => 'text',
                'label' => 'Bottom description',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 101,
                'visible' => true,
                'wysiwyg_enabled' => 1,
                'visible_on_front' => 1,
                'is_html_allowed_on_front' => 1,
                'user_defined' => true,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group' => 'General Information',
            ]
        );
        $idg = $categorySetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'General Information');
        $categorySetup->addAttributeToGroup(
            $entityTypeId,
            $attributeSetId,
            $idg,
            'bottom_description',
            46
        );

        //$installer->updateAttribute($entityTypeId, 'bottom_description', 'is_wysiwyg_enabled', 1);
        //$installer->updateAttribute($entityTypeId, 'bottom_description', 'is_html_allowed_on_front', 1);

        $installer->endSetup();
    }

Read more here.

11
  • you also need to add the field in the form using the ui component.
    – Marius
    Jun 7, 2016 at 13:47
  • @Vasilii - from the comments of your link it says that this code does not work after Mage 2.0.2. We are currently at 2.0.4 and 2.0.7.
    – Max
    Jun 7, 2016 at 13:53
  • @Marius: could you please elaborate?
    – Max
    Jun 7, 2016 at 13:54
  • You can see here how the fields are added in the category add/edit form. You need to create a file called view/adminhtml/ui_component/category_form.xml inside your module where you add your new field using the similar markup as in the file I just linked.
    – Marius
    Jun 7, 2016 at 13:56
  • Do I need the common plugin files in my module folder, like registration.php and stuff?
    – Max
    Jun 7, 2016 at 14:15
0

Create the module in app/code/Max/CategorySeo folder and create below files in your custom module:

  1. Setup/Patch/Data/AddSeoDescriptionCategoryAttribute.php

     <?php
    
     declare(strict_types=1);
    
     namespace Max\CategorySeo\Setup\Patch\Data;
    
     use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
     use Magento\Eav\Setup\EavSetup;
     use Magento\Eav\Setup\EavSetupFactory;
     use Magento\Framework\Setup\ModuleDataSetupInterface;
     use Magento\Framework\Setup\Patch\DataPatchInterface;
     use Magento\Framework\Setup\Patch\PatchRevertableInterface;
    
     class AddSeoDescriptionCategoryAttribute implements DataPatchInterface, PatchRevertableInterface {
    
     /**
      * @var ModuleDataSetupInterface
      */
     private $moduleDataSetup;
     /**
      * @var EavSetupFactory
      */
     private $eavSetupFactory;
    
     /**
      * Constructor
      *
      * @param ModuleDataSetupInterface $moduleDataSetup
      * @param EavSetupFactory $eavSetupFactory
      */
     public function __construct(
         ModuleDataSetupInterface $moduleDataSetup,
         EavSetupFactory $eavSetupFactory
     ) {
         $this->moduleDataSetup = $moduleDataSetup;
         $this->eavSetupFactory = $eavSetupFactory;
     }
    
     /**
      * {@inheritdoc}
      */
     public function apply()
     {
         $this->moduleDataSetup->getConnection()->startSetup();
         /** @var EavSetup $eavSetup */
         $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
         $eavSetup->addAttribute(
             \Magento\Catalog\Model\Category::ENTITY,
             'seo_description',
             [
                 'type' => 'varchar',
                 'label' => 'SEO Description',
                 'input' => 'text',
                 'sort_order' => 333,
                 'source' => '',
                 'global' => ScopedAttributeInterface::SCOPE_STORE,
                 'visible' => true,
                 'required' => false,
                 'user_defined' => false,
                 'default' => null,
                 'group' => 'General Information',
                 'backend' => ''
             ]
         );
    
         $this->moduleDataSetup->getConnection()->endSetup();
     }
    
     public function revert()
     {
         $this->moduleDataSetup->getConnection()->startSetup();
         /** @var EavSetup $eavSetup */
         $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
         $eavSetup->removeAttribute(\Magento\Catalog\Model\Category::ENTITY, 'seo_description');
    
         $this->moduleDataSetup->getConnection()->endSetup();
     }
    
     /**
      * {@inheritdoc}
      */
     public function getAliases()
     {
         return [];
     }
    
     /**
      * {@inheritdoc}
      */
     public static function getDependencies()
     {
         return [
    
         ];
     }
    

    }

  2. view/adminhtml/ui_component/category_form.xml

    <?xml version="1.0" ?>
    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
     <fieldset name="general">
         <field name="seo_description">
             <argument name="data" xsi:type="array">
                 <item name="config" xsi:type="array">
                     <item name="required" xsi:type="boolean">false</item>
                     <item name="validation" xsi:type="array">
                         <item name="required-entry" xsi:type="boolean">false</item>
                     </item>
                     <item name="sortOrder" xsi:type="number">333</item>
                     <item name="dataType" xsi:type="string">string</item>
                     <item name="formElement" xsi:type="string">input</item>
                     <item name="label" xsi:type="string" translate="true">SEO Description</item>
                 </item>
             </argument>
         </field>
     </fieldset>
    </form>
    
  3. registration.php

    <?php
    
    use Magento\Framework\Component\ComponentRegistrar;
    
    ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Max_CategorySeo', __DIR__);
    
  4. etc/module.xml

    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
     <module name="Max_CategorySeo">
         <sequence>
             <module name="Magento_Catalog"/>
         </sequence>
     </module>
    </config>
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.