changeling
changeling

Reputation: 803

Magento: Adding element to adminhtml form (Override object in core class)

Hello I want to add one field to the edit customer form of magento. This field in functionality terms is just a trigger to do several things on my extension. I was trying to do it by overriding the class adding a field set to a new form object and then calling the parent init form. This does not work. I was wondering how to modify an object on the parent class. So far i have:

            <adminhtml>
            <rewrite>
                <customer_edit_tab_account>Yougento_S2b_Block_Rewrite_Editcustomer</customer_edit_tab_account>
            </rewrite>
        </adminhtml>  

This is to set the rewrite, then my class (block/rewrite/editcustomer.php) is

    class Namespace_Module_Block_Rewrite_Editcustomer extends Mage_Adminhtml_Block_Customer_Edit_Tab_Account
{
    public function initForm()
    {


        $form = new Varien_Data_Form();
        $fieldset = $form->addFieldset('new_fieldset',
                array('legend'=>Mage::helper('customer')->__('Extra options'))
        );
        $fieldset->addField('newattr', 'text',
            array(
                'label' => Mage::helper('customer')->__('Select type'),
                'class' => 'input-text',
                'name'  => 'newattr',
                'required' => false
            )
        );
        $this->setForm($form);
        return parent::initForm();
    }
}  

My code is executed, but i guess that when i call the parent the new form object replaces the new one instead of extending it. This may be more of a php question but i thought i would specify its for magento

Upvotes: 1

Views: 5579

Answers (1)

Magento Guy
Magento Guy

Reputation: 2493

The problem that you're having is because you're calling parent::initForm() after setting the form. This creates a new form and overwrites the one you passed to ->setForm().

Consider writing it this way:

public function initForm()
{
    parent::initForm();
    $form = $this->getForm();
    $fieldset = $form->addFieldset('new_fieldset',
            array('legend'=>Mage::helper('customer')->__('Extra options'))
    );
    $fieldset->addField('newattr', 'text',
        array(
            'label' => Mage::helper('customer')->__('Select type'),
            'class' => 'input-text',
            'name'  => 'newattr',
            'required' => false
        )
    );
    $this->setForm($form);
    return $this;
}

Upvotes: 4

Related Questions