Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend Magento's EAV Attribute Model

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".

like image 273
user215421 Avatar asked Apr 26 '11 06:04

user215421


People also ask

What are EAV attributes in Magento 2?

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).

What is extension attribute?

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.

In which type of setup script do you create the EAV attribute?

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).


1 Answers

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.

(1) Create your new DB field (is_visible_in_category_list)

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.

(2) Add the field to the edit product attribute page

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.

like image 62
Anders Thirsgaard Rasmussen Avatar answered Oct 13 '22 14:10

Anders Thirsgaard Rasmussen