Add site field using event watcher in magento admin privileges

I am creating a module that allows the user to select a website during the creation of a role permission (System -> Permission -> Role -> Add New role -> Role Resource). I am using an observer to achieve this, but I cannot get the form object.

Observer.php

class Mymodule_Mycompany_Model_Observer 
{ 
    public function appendCustomRow(Varien_Event_Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();
        if (!isset($block)) {
            return $this;
        }
        if ($block->getType() == 'adminhtml/permissions_editroles') {       
            //get form instance
            $form = $observer->getEvent()->getForm();
            //create new custom fieldset 'website'
            $fieldset = $form->addFieldset('website', array(
                'legend' => 'Website Extras',
                'class' => 'fieldset-wide'
                )
            );
            //add new field
            $fieldset->addField('website', 'text', array(
                'name'      => 'website',
                'label'     => Mage::helper('adminhtml')->__('Website'),
                'title'     => Mage::helper('adminhtml')->__('Website'),
                'disabled'  => false,
            ));
        }
    }
}

      

MyModule / MyCompany / etc. /config.xml

<adminhtml>
         <events>
            <core_block_abstract_prepare_layout_before>
                <observers>
                    <Event_column_append>
                        <type>model</type>
                        <class>Mymodule_Mycompany_Model_Observer</class>
                        <method>appendCustomColumn</method>
                    </Event_column_append>
                </observers>
            </core_block_abstract_prepare_layout_before>
          </events>
</adminhtml>

      

+3


source to share


1 answer


I did it. Here is a modified piece of code that works

public function appendCustomColumn(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();
    if (!isset($block)) {
        return $this;
    }
    if ($block->getType() == 'adminhtml/permissions_tab_roleinfo') {       
        //get form instance
        $form = $block->getForm();
        //create new custom fieldset 'website'
        $fieldset = $form->addFieldset(
            'website_field', 
            array(
                'legend' => 'Website Extras',
                'class' => 'fieldset-wide'
            )
        );
        //add new field
        $fieldset->addField('website', 'text', array(
            'name'      => 'website',
            'label'     => Mage::helper('adminhtml')->__('Website'),
            'title'     => Mage::helper('adminhtml')->__('Website'),
            'disabled'  => false,
        ));
    }
}

      

XML configuration:

<events>
    <adminhtml_block_html_before>
        <observers>
            <Event_column_append>
                <type>model</type>
                <class>Mymodule_Mycompany_Model_Observer</class>
                <method>appendCustomColumn</method>
            </Event_column_append>
        </observers>
    </adminhtml_block_html_before>
</events>

      



  • You need to use the "adminhtml_block_html_before" event because the event you were using didn't even create the form object. This is why you couldn't access the form object.
  • The block that creates these form elements is "adminhtml / permissions_tab_roleinfo".
  • The method you used was not the same as in the observer.

Hope it helps :) ...

Cheers, Swapna

+7


source







All Articles