Add a Conditions Tab to a Magento Form

The Magento Shopping Cart Price Rules section in the admin contains a tab where you can configure conditions for a rule to apply. You can add a conditions tab to a Magento form as part of custom functionality by following the below steps.

If you’ve followed the posts involving creating an admin grid and form section within the admin, you’ll be ready to start adding a tab containing conditions.

Within your adminhtml controller that’s responsible for handling the routing for the form, within the editAction() method, add the code below to render the Edit.php form container block class, and the Tabs.php tab class.

// app/code/local/[Vendor]/[Module]/controllers/Adminhtml/EntityController.php

<?php
class [Vendor]_[Module]_Adminhtml_EntityController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        ....
    }

    public function editAction()
    {
        $model = Mage::getModel('[vendor]_[module]/entity');
        $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
        if (!empty($data)) {
            $model->addData($data);
        }

        $model->getConditions()->setJsFormObject('entity_conditions_fieldset');
        Mage::register('entity', $model);

        $this->loadLayout()
            ->_addContent($this->getLayout()->createBlock('[vendor]_[module]/adminhtml_entity_edit'))
            ->_addLeft($this->getLayout()->createBlock('[vendor]_[module]/adminhtml_entity_edit_tabs'))
            ->renderLayout();
    }
}

The line $model->getConditions()->setJsFormObject('entity_conditions_fieldset'); is code that is specific for adminhtml forms in Magento that contain conditions.

getConditions() is a method that belongs in Mage_Rule_Model_Abstract, therefore your model should extend this class.

// app/code/local/[Vendor]/[Module]/Model/Entity.php

<?php
class [Vendor]_[Module]_Model_Entity extends Mage_Rule_Model_Abstract
{
    public function _construct()
    {
        $this->_init('[vendor]_[module]/entity');
    }

    public function getConditionsInstance()
    {
        return Mage::getModel('salesrule/rule_condition_combine');
    }

    public function getActionsInstance()
    {
        return parent::getActionsInstance();
    }
}

Extending the abstract Mage_Rule_Model_Abstract class means that your model will need to define the getConditionsInstance() and getActionsInstance() methods.

getConditionsInstance() will return the conditions model that will return the conditions to choose from on the form. As actions aren’t included in this example, getActionsInstance() can simply return the parent getActionsInstance() method.

Within Tabs.php, the _beforeToHtml() method is responsible for loading the Conditions.php tab.

// app/code/local/[Vendor]/[Module]/Block/Adminhtml/Entity/Edit/Tabs.php

<?php
class [Vendor]_[Module]_Block_Adminhtml_Entity_Edit_Tabs extends Mage_Adminhtml_Block_Widget_Tabs
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('form_info_tabs');
        $this->setDestElementId('edit_form');
    }

    protected function _beforeToHtml()
    {
        $this->addTab('general', array(
            'label'     => Mage::helper('[vendor]_[module]')->__('Conditions'),
            'title'     => Mage::helper('[vendor]_[module]')->__('Conditions'),
            'content'   => $this->getLayout()->createBlock('[vendor]_[module]/adminhtml_entity_edit_tab_conditions')->toHtml(),
            'active'    => true
        ));

        return parent::_beforeToHtml();
    }
}

The conditions tab implements Mage_Adminhtml_Block_Widget_Tab_Interface and therefore contains methods such as getTabLabel() and getTabTitle().

The most important lines of code are within the _prepareForm() method.

// app/code/local/[Vendor]/[Module]/Block/Adminhtml/Entity/Edit/Tab/Conditions.php

<?php
class [Vendor]_[Module]_Block_Adminhtml_Entity_Edit_Tab_Conditions extends Mage_Adminhtml_Block_Widget_Form
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
    /**
     * Prepare content for tab
     *
     * @return string
     */
    public function getTabLabel()
    {
        return Mage::helper('[vendor]_[module]')->__('Conditions');
    }

    /**
     * Prepare title for tab
     *
     * @return string
     */
    public function getTabTitle()
    {
        return Mage::helper('[vendor]_[module]')->__('Conditions');
    }

    /**
     * Returns status flag about this tab can be showen or not
     *
     * @return true
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Returns status flag about this tab hidden or not
     *
     * @return true
     */
    public function isHidden()
    {
        return false;
    }

    protected function _prepareForm()
    {
        $model = Mage::getModel('[vendor]_[module]/entity');
        $form = new Varien_Data_Form();
        $model->getConditions()->setJsFormObject('entity_conditions_fieldset');

        $form->setHtmlIdPrefix('overlay_');


        $renderer = Mage::getBlockSingleton('adminhtml/widget_form_renderer_fieldset')
            ->setTemplate('promo/fieldset.phtml')
            ->setNewChildUrl($this->getUrl('*/promo_quote/newConditionHtml/form/rule_conditions_fieldset'));

        $fieldset = $form->addFieldset('conditions_fieldset', array(
                'legend'=>Mage::helper('catalogrule')->__('Conditions (leave blank for all products)'))
        )->setRenderer($renderer);

        $fieldset->addField('conditions', 'text', array(
            'name' => 'conditions',
            'label' => Mage::helper('catalogrule')->__('Conditions'),
            'title' => Mage::helper('catalogrule')->__('Conditions'),
            'required' => true,
        ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions'));

        $form->setValues($model->getData());

        $this->setForm($form);

        return parent::_prepareForm();
    }
}

After following the above steps, you should have the conditions from the Mage_SalesRule module loaded in.

Add a Conditions Tab to a Magento Form

Note: This article is based on Magento Open Source version 1.9.