skip to Main Content

I am trying to add a custom attribute for categories, i have got it to display on the backend but when I press save nothing is happening. I have checked the eav_attribute table and it is not inserting!

/Setup/InstallData.php

<?php
namespace XXXXSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

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]);

        //Category Attribute Create Script
        $eavSetup->addAttribute(
            MagentoCatalogModelCategory::ENTITY,
            'category_front_label',
            [
                'group' => 'autosmart_category_fields',
                'label' => 'Category Short Description',
                'type'  => 'text',
                'input' => 'textarea',
                'required' => false,
                'sort_order' => 1,
                'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_STORE,
                'used_in_product_listing' => true,
                'visible_on_front' => false
            ]
        );

        $setup->endSetup();
    }
}

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="general">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">General Settings</item>
            <item name="collapsible" xsi:type="boolean">true</item>
            <item name="sortOrder" xsi:type="number">0</item>
        </item>
    </argument>
    <field name="category_front_label">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortOrder" xsi:type="number">5000</item>
                <item name="dataType" xsi:type="string">string</item>
                <item name="formElement" xsi:type="string">textarea</item>
                <item name="label" xsi:type="string" translate="true">Product Label</item>
            </item>
        </argument>
    </field>
</fieldset>
</form>

I then run (in this order)

bin/magento setup:di:compile

bin/magento setup:upgrade

bin/magento cache:clean

It is displaying as a field in the back end but not saving, presumably because the install script isn’t creating the entry in the eav_attribute table. Been stuck on this issue for a few days.

3

Answers


  1. Chosen as BEST ANSWER

    I needed to remove the module from the setup_module before running compile and upgrade otherwise install schema didn't work


  2. Had a very similar problem. I was missing the namespace XXXXSetup; at the beginning of /Setup/InstallData.php, so make sure to include it in your PHP class file.

    Login or Signup to reply.
  3. I had a similar problem, but my plugin already included an installschema. So instead of InstallData.php I had to setup an UpgradeData.php script in my setup file. This worked.

    <?php
    
    namespace XXXXXXSetup;
    
    use MagentoEavSetupEavSetup;
    use MagentoFrameworkSetupUpgradeDataInterface;
    use MagentoCatalogModelCategory;
    use MagentoEavSetupEavSetupFactory;
    use MagentoFrameworkSetupModuleContextInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    
    /**
     * Catalog Data Upgrade
     */
    class UpgradeData implements UpgradeDataInterface
    {
        /**
         * EAV setup factory
         *
         * @var EavSetupFactory
         */
        private $eavSetupFactory;
    
        /**
         * Class Constructor
         *
         * @param EavSetupFactory     $eavSetupFactory     Eav setup factory.
        public function __construct(
            EavSetupFactory $eavSetupFactory
        )
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }
    
        /**
         * Upgrade the module data.
         *
         * @param ModuleDataSetupInterface $setup   The setup interface
         * @param ModuleContextInterface   $context The module Context
         *
         * @return void
         */
        public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            $setup->startSetup();
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    
            if (version_compare($context->getVersion(), '1.1.0', '<')) {
                $eavSetup->addAttribute(
                    MagentoCatalogModelCategory::ENTITY,
                    'xx',
                    [
                        'type'         => 'int',
                        'label'        => '',
                        'input'        => 'select',
                        'sort_order'   => 100,
                        'source'       => 'MagentoEavModelEntityAttributeSourceBoolean',
                        'global'     => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                        'visible'      => true,
                        'required'     => false,
                        'user_defined' => true,
                        'default'      => '0',
                        'group'        => 'Display Settings',
                        'position' => 999
                    ]
                );
            }
    
            $setup->endSetup();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search