beingalex
beingalex

Reputation: 2476

Create dependant attributes in install script in Magento

In the administration area of Magento I am trying to create a dependant field. A dependant field is one that only becomes available/enabled depending on the value of, let's say, a drop down with 'Yes' or 'No' values. This is a built in feature of Magento as you can see from this blog post.

However the above blog post (and others I have found) assumes the fields are getting added in system.xml or using the method Vikram outlined below but I would like to add my dependency in my modules install script when I define my attributes, for example:

$installer->addAttribute(
    'catalog_category', 
    'show_dependant', 
    array(
        'label' => 'Show dependant?',
        'group' => 'My Group',
        'type' => 'int',
        'input' => 'select',
        'source'  => 'eav/entity_attribute_source_boolean',
        'required' => false,
        'visible' => true,
        'default' => '0',
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    )
);


$installer->addAttribute(
    'catalog_category',
    'my_attribute_name',
    array(
        'label' => 'A New Attribute',
        'group' => 'My Group',   //will be created if necessary
        'type'  => 'int',
        'class' => 'validate-number',
        'required' => false,

        // Would be something like this maybe?
        'depends' => array('show_dependant', 1)

    )
);

Anyone know if this is even possible?

Upvotes: 0

Views: 1178

Answers (2)

Sergey Korzhov
Sergey Korzhov

Reputation: 182

I have created simple category attribute dependency by adding new input renderer for attribute. It is working this way: You have several attributes:

– my_attribute
– my_attribute_text
– my_attribute_select

Note that they all start from my_attribute.

First attribute has boolean type. When it is set to true – other attributes that start from my_attribute is visible.

Source - https://github.com/elpas0/category_dependence

Description - http://nwdthemes.com/2015/02/20/magento-category-attributes-dependency/

Upvotes: 0

Slimshadddyyy
Slimshadddyyy

Reputation: 4073

If you are working in MAGENTO ADMIN FORMS, consider this example for showing a text field only when the 'Specified' option is chosen. This method used admin form and not system.xml method.

$form = new Varien_Data_Form();

$form->addField('yesno', 'select', array(
    'label'  => $this->__('Yes or No?'),
    'values' => Mage::model('adminhtml/system_config_source_yesnocustom')
        ->toOptionArray(),
));
$form->addField('custom_value', text, array(
    'label'  => $this->__('Other'),
));

// Append dependency javascript
$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('yesno', 'yesno')
        ->addFieldMap('custom_value', 'custom_value')
        ->addFieldDependence('custom_value', 'yesno', 2) // 2 = 'Specified'
);

You can add as many field mappings and field dependencies in this way as you wish.

Upvotes: 1

Related Questions