7

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

7
  • sure it's possible just tell what you mean by static field
    – Anton S
    Commented Apr 26, 2011 at 6:45
  • attribute as static field means a new column in the table. here i want to add a new column in the table eav_attribute
    – user215421
    Commented Apr 26, 2011 at 7:07
  • my friend you misunderstand the whole point of EAV then as it is ment to scale the field limit to row level by storing the meta information of needed attributes in different tables. To make your point please describe what value this will give and for what this will be useful
    – Anton S
    Commented Apr 26, 2011 at 7:28
  • Sorry, i dont misunderstand something ;) Magento gives to possibilities to extend a model, with static fields which are also attributes and with "normal" attributes. static fields are attributes located in the "model-table". now what i want: i want to extend the eav-attribute, not a model that use the EAV! the eav attribute should get a new setting, like "visible in cvategory list" (admin catalog manage attributes).
    – user215421
    Commented Apr 26, 2011 at 7:41
  • now please edit your question to reflect the last bit of your last comment
    – Anton S
    Commented Apr 26, 2011 at 7:46

1 Answer 1

12

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.