I would like to add a new attribute to Magento's EAV attribute model. Is this possible?
I know that Magento lets you extend models using static fields (which are on the entity table), but I would like to add a new field to the EAV attribute table itself (for catalog product attributes). The new attribute will be a new setting, similar to "Visible in Category List".
EAV (Entity-attribute-value) is a model of storing the values of entity attributes in a certain data storage. As a data storage, Magento 2 supports MySQL-compatible databases (like MySQL, MySQL NDB Cluster, MariaDB, Percona and others).
Extension attributes. Extension attributes are new in Magento 2. They are used to extend functionality and often use more complex data types than custom attributes. These attributes do not appear in the Admin.
You can create attributes through the setup script (Vendor\Module\Setup\InstallData or Vendor\Module\Setup\UpgradeData) or in the admin panel (for products only).
To add a new setting for product attributes, you can create an extension that (1) adds the new column to the catalog/eav_attribute table, and (2) puts in a field for the new setting in the attribute edit page using an observer.
Make an SQL script for your extension, and add the new column. I would recommend using the catalog/eav_attribute table, but you could probably use eav_attribute as well:
$installer = $this;
$installer->startSetup();
$table = $installer->getTable('catalog/eav_attribute');
$installer->getConnection()->addColumn(
$table,
'is_visible_in_category_list',
"TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'"
);
$installer->getConnection()->addKey(
$table,
'IDX_VISIBLE_IN_CATEGORY_LIST',
'is_visible_in_category_list'
);
$installer->endSetup();
Here, I also added an index for faster querying.
An event is fired when preparing the attribute edit form, so let's observe it:
<events>
<adminhtml_catalog_product_attribute_edit_prepare_form>
<observers>
<is_visible_in_category_list_observer>
<class>mymodule/observer</class>
<method>addVisibleInCategoryListAttributeField</method>
</is_visible_in_category_list_observer>
</observers>
</adminhtml_catalog_product_attribute_edit_prepare_form>
</events>
Then, add the new field in the observer:
public function addVisibleInCategoryListAttributeField($observer)
{
$fieldset = $observer->getForm()->getElement('base_fieldset');
$attribute = $observer->getAttribute();
$fieldset->addField('is_visible_in_category_list', 'select', array(
'name' => 'is_visible_in_category_list',
'label' => Mage::helper('mymodule')->__('Visible in Category List'),
'title' => Mage::helper('mymodule')->__('Visible in Category List'),
'values' => Mage::getModel('adminhtml/system_config_source_yesno')->toOptionArray(),
));
}
That's all. Saving the setting from the edit page is automatically handled because the field name in the form matches the DB field name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With