Reputation: 43
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 XX\XX\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
{
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(
\Magento\Catalog\Model\Category::ENTITY,
'category_front_label',
[
'group' => 'autosmart_category_fields',
'label' => 'Category Short Description',
'type' => 'text',
'input' => 'textarea',
'required' => false,
'sort_order' => 1,
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::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.
Upvotes: 2
Views: 1845
Reputation: 622
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 XXX\XXX\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Catalog\Model\Category;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* 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(
\Magento\Catalog\Model\Category::ENTITY,
'xx',
[
'type' => 'int',
'label' => '',
'input' => 'select',
'sort_order' => 100,
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'required' => false,
'user_defined' => true,
'default' => '0',
'group' => 'Display Settings',
'position' => 999
]
);
}
$setup->endSetup();
}
}
Upvotes: 0
Reputation: 51
Had a very similar problem. I was missing the namespace XX\XX\Setup;
at the beginning of /Setup/InstallData.php
, so make sure to include it in your PHP class file.
Upvotes: 2
Reputation: 43
I needed to remove the module from the setup_module before running compile and upgrade otherwise install schema didn't work
Upvotes: 1