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
I needed to remove the module from the setup_module before running compile and upgrade otherwise install schema didn't work
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.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.