0

There are quite a few questions similar to this one, but I can't get any answers to work.

I run Magento 2.4.2 and tried to add a custom attribute with the instruction here:

https://devdocs.magento.com/guides/v2.4/ui_comp_guide/howto/add_category_attribute.html

I have created the two required files in my module (InstallData.php and category_form.xml) with the following differences:

InstallData.php:

     public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
     {
         $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
-        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'attribute_id', [
+        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'myattribute', [
             'type'     => 'int',
-            'label'    => 'Your Category Attribute Name',
+            'label'    => 'My Category Attribute Name',
             'input'    => 'boolean',
             'source'   => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
             'visible'  => true,
 <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
     <fieldset name="display_settings">
-        <field name="attribute_id">
+        <field name="myattribute">
             <argument name="data" xsi:type="array">
                 <item name="config" xsi:type="array">
                     <item name="dataType" xsi:type="string">boolean</item>
                     <item name="formElement" xsi:type="string">checkbox</item>
-                    <item name="label" xsi:type="string" translate="true">Your Category Attribute Name</item>
+                    <item name="label" xsi:type="string" translate="true">My Category Attribute Name</item>
                     <item name="prefer" xsi:type="string">toggle</item>
                     <item name="valueMap" xsi:type="array">
                         <item name="true" xsi:type="string">1</item>

After running bin/magento setup:upgrade and bin/magento c:c the user interface shows the checkbox:

new checkbox

So the overall procedure went fine. But now I try to change the value of the checkbox and save the default category by clicking on the red "Save" button on the top right, the checkbox state is not saved and restored back to off.

Where can I start looking for the error?

1
  • Hi you can resoled you problem?, I'm following this tutorial mageplaza.com/devdocs/… but not work, create the field but not saved information to display in view of category
    – Mike Peña
    Commented Aug 27, 2021 at 17:13

2 Answers 2

0

I have just created an attribute and add it to the category form. Please check the below file.

InstallData.php

<?php
namespace VendorName\ModuleName\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;

class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**  
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    
    /**  
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /**
         * Adding attribute myattribute in the category
         */
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'myattribute',
            [
                'type' => 'int',
                'label' => 'Custom Attribute',
                'input' => 'select',
                'sort_order' => 333,
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => null,
                'group' => 'General Information',
                'backend' => ''
            ]
        );
    }
}

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="myattribute">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">30</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="label" xsi:type="string" translate="true">Custom Attribute</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>
            </argument>
        </field>
    </fieldset>
</form>

Please check and let me still if you facing any issues.

6
  • I see the custom attribute in the “General” section, but I can't save its state. It always gets back to "no".
    – topskip
    Commented Mar 11, 2021 at 14:35
  • Please given the code and check. It's working fine in Magento setup.
    – ZealousWeb
    Commented Mar 11, 2021 at 14:36
  • I have used your code and run bin/magento setup:upgrade afterwards
    – topskip
    Commented Mar 11, 2021 at 14:37
  • did you get any errors? Please check logs.
    – ZealousWeb
    Commented Mar 11, 2021 at 14:38
  • Because no need to do anything else to save the category field value. Please try in the fresh setup so you will get more idea.
    – ZealousWeb
    Commented Mar 11, 2021 at 14:39
0

InstallData.php file <?php namespace WEBO\CustomCategoryField\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\Setup\CategorySetupFactory;

class InstallData implements InstallDataInterface { /** * Category setup factory * * @var CategorySetupFactory */ private $categorySetupFactory;

/**
 * EAV setup factory
 *
 * @var EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Category::ENTITY);
    $attributeSetId = $eavSetup->getDefaultAttributeSetId($entityTypeId);

    // Add Custom Text Attribute
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'WEBO_CustomText_Attribute',
        [
            'type' => 'varchar',
            'label' => 'WEBO Custom Text',
            'input' => 'text',
            'sort_order' => 10,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'group' => 'General Information',
        ]
    );

    // Add Custom Image Attribute
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'WEBO_CustomImage_Attribute',
        [
            'type' => 'varchar',
            'label' => 'WEBO Custom Image',
            'input' => 'image',
            'sort_order' => 40,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'group' => 'General Information',
            'backend' => 'Magento\Catalog\Model\Category\Attribute\Backend\Image'
        ]
    );

    // Add Short Description Attribute
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Category::ENTITY,
        'description',
        [
            'type' => 'text',
            'label' => 'WEBO Short Description',
            'input' => 'textarea',
            'sort_order' => 50,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'group' => 'General Information',
            'wysiwyg_enabled' => true,
            'is_html_allowed_on_front' => true
        ]
    );


    $idg = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, 'General Information');
    $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $idg, 'WEBO_CustomImage_Attribute', 40);
    $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $idg, 'WEBO_CustomText_Attribute', 10);
    $eavSetup->addAttributeToGroup($entityTypeId, $attributeSetId, $idg, 'description', 50);
}

}

<?xml version="1.0" ?>
data.category category_form true Custom Attributes
  <field name="WEBO_CustomText_Attribute" sortOrder="10" formElement="input">
      <settings>
          <dataType>string</dataType>
          <label translate="true">Custom Text</label>
          <visible>true</visible>
          <validation>
              <rule name="required-entry" xsi:type="boolean">false</rule>
          </validation>
      </settings>
  </field>
     <field name="WEBO_CustomImage_Attribute" sortOrder="40" formElement="imageUploader">
       <argument name="data" xsi:type="array">
           <item name="config" xsi:type="array">
               <item name="source" xsi:type="string">category</item>
           </item>
       </argument>
       <settings>
           <elementTmpl>ui/form/element/uploader/image</elementTmpl>
           <dataType>string</dataType>
           <label translate="true">Custom Image</label>
           <visible>true</visible>
           <required>false</required>
       </settings>
       <formElements>
           <imageUploader>
               <settings>
                   <required>false</required>
                   <uploaderConfig>
                       <param xsi:type="url" name="url" path="catalog/category_image/upload"/>
                   </uploaderConfig>
                   <previewTmpl>Magento_Catalog/image-preview</previewTmpl>
                   <openDialogTitle>Media Gallery</openDialogTitle>
                   <initialMediaGalleryOpenSubpath>catalog/category</initialMediaGalleryOpenSubpath>
                   <allowedExtensions>jpg jpeg gif png</allowedExtensions>
                   <maxFileSize>4194304</maxFileSize>
               </settings>
           </imageUploader>
       </formElements>
   </field>
 
  <field name="description" template="ui/form/field" sortOrder="50" formElement="wysiwyg">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="wysiwygConfigData" xsi:type="array">
                    <item name="height" xsi:type="string">100px</item>
                    <item name="add_variables" xsi:type="boolean">false</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>
            <label translate="true"> Short Description</label>
            <dataScope>description</dataScope>
        </settings>
        <formElements>
            <wysiwyg class="Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg">
                <settings>
                    <rows>8</rows>
                    <wysiwyg>true</wysiwyg>
                </settings>
            </wysiwyg>
        </formElements>
    </field>
1
  • this code is adding the custom attributes with the fieldset and fields in the category_form page. but it is not saving the custom attributes inthe database table.. what is the issue here? Commented Jun 7 at 4:27

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.