I am writing a Magento 2 module - am trying to get it to add a custom attribute to every product. My Setup>InstallData.php file is successfully adding the attribute to the eav_attribute table when installing the module, and the adminhtml is rendering the field as having name=product[my_attribute], which I think means that it "accepts" the field as being a valid part of the model. However, when attempting to save the product with a value in the custom attribute, nothing is saved in the database.

here is my code:


// module namespace is 'Duel', module name is 'Gallery'
namespace Duel\Gallery\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

public function __construct(EavSetupFactory $eavSetupFactory)
    $this->_eavSetupFactory = $eavSetupFactory;

public function install(ModuleDataSetupInterface $setup, 
ModuleContextInterface $context)
/** @var EavSetup $eavSetup */

$eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);

* Add attributes to the eav/attribute

        'type' => 'text',
        'backend' => '',
        'label' => 'My New Attribute',
        'input' => 'text',
        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
        'visible' => true,
        'user_defined' => true,
        'required' => false,
        'visible_on_front' => true

and here is the html in view>adminhtml>ui_component>product_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<fieldset name="attributes">
<argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="label" xsi:type="string" translate="true">MY CUSTOM ATTRIBUTE</item>
        <item name="collapsible" xsi:type="boolean">true</item>
        <item name="sortOrder" xsi:type="number">200</item>
<field name="my_attribute">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true">My new attribute</item>
            <item name="formElement" xsi:type="string">textarea</item>

I briefly had it saving after manually changing a couple of fields of 'my_attribute' in the eav_attribute table, however I committed the code but stupidly didn't save the exact mysql database state and now can't get it to work again (upon testing that it creates a working form every time the module is installed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.