Magento – Field dependency for category attributes

adminformattributescategory-attributedependencysetup-script

I add attributes in setup script, all is configured properly and attributes are added to a new tab called "MyGroup":

$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category', 'my_attribute1', array(
    'group'         => 'MyGroup',
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'My Attribute 1',        
    'backend'       => '',
    'visible'       => 1,   
    'required'      => 0,
    'user_defined' => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,     
));
//...here add more attributes
$installer->endSetup();

This automatically creates a tab "MyGroup" and a form with fields for all the attributes which I defined in setup script.

Is there a way to have more control over the entire form inside "MyGroup" tab? For example, normally when I create an admin form I can also add dependency for fields (e.g. to hide field2 if no option selected in field1 etc.). I know how to add field dependency in custom admin form (e.g. http://www.atwix.com/magento/fields-dependency-in-magento-admin-forms/) but I don't know how to do this with category attributes.

So is there a way to override that form somehow? Or at least to add field dependency?

Best Answer

Bad news: You cannot define dependencies in attributes, so yes, you will need to manipulate the form directly like in the linked article(s).

Good news: No rewrites are required, there is an observer that you can use: adminhtml_catalog_category_edit_prepare_form

It's triggered for each group in Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes::_prepareForm() and has the Varien_Data_Form as parameter, that you can access with:

$form = $observer->getForm()

At this point all attributes are already added to a fieldset called fieldset_group_{$groupId} - by checking for the fieldset you can identify the currently rendered tab. You need to know the group ID of your tab. Unfortunately the group object itself is not passed to the observer.

Related Topic