Categrory attribute not saved
-
13-12-2020 - |
Question
Ive added my own color attribute for categories following this guide:
Magento 2 DevDocs: How to add a Category Attribute
But when I save my category my field is not saved.
First I created a InstallData.php
:
use Magento\Framework\Setup\{
ModuleContextInterface,
ModuleDataSetupInterface,
InstallDataInterface
};
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory) {
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'color', [
'type' => 'varchar(5)',
'label' => 'Color',
'input' => 'text',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Text',
'visible' => true,
'default' => '',
'required' => false,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'group' => 'Display Settings',
]);
}
}
Then Ive added my 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="display_settings">
<field name="color" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">color</item>
<item name="label" xsi:type="string" translate="true">Farbe</item>
<item name="default" xsi:type="string"></item>
</item>
</argument>
</field>
</fieldset>
</form>
Then I run:
php bin/magento setup:upgrade
php bind/magento setup:di:compile
The Field is showing up in the backend at the right place but when I enter a value and click save the value is not saved.
So can anyone tell me whats wrong with my script?
Solution
you have added data type 'type' => 'varchar(5)',
in your script, so please try to add less then 5 character into input and then check your value is save or not.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange