1

I would like to add a button to the admin interface for a module to perform some custom actions, at the moment the observer isn't being picked up once the admin interface loads. here is my config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Ds_Imports>
            <version>1.0.0</version>
        </Ds_Imports>
    </modules>

    <global>
        <adminhtml>

            <events>
                <adminhtml_widget_container_html_before>
                    <observers>
                        <add_button_test>
                            <type>singleton</type>
                            <class>Ds_Imports_Model_Observer</class>
                            <method>addButtonTest</method>
                        </add_button_test>
                    </observers>
                </adminhtml_widget_container_html_before>
            </events>

            <layout>
                <updates>
                    <ds_imports>
                        <file>importer.xml</file>
                    </ds_imports>
                </updates>
            </layout>
        </adminhtml>
        <helpers>
            <imports>
                <class>Ds_Imports_Helper</class>
            </imports>
        </helpers>
        <blocks>
            <imports>
                <class>Ds_Imports_Block</class>
            </imports>
            <!-- <adminhtml>
                <rewrite>
                    <sales_order_grid>Ds_Imports_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml> -->
        </blocks>
        <models>
            <imports>
                <class>Ds_Imports_Model</class>
                <resourceModel>imports_resource</resourceModel>
            </imports>
        </models>
        <resources>
            <imports_setup>
                <setup>
                    <module>Ds_Imports</module>
                </setup>
            </imports_setup>
        </resources>

    </global>
     <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <ds_imports before="Mage_Adminhtml">Ds_Imports_Adminhtml</ds_imports>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
</config>

and this is the observer it is trying to load

<?php

class Ds_Imports_Model_Observer extends Varien_Event_Observer 
{

    public function __construct() {

    }

    public function addButtonTest($observer) {
        echo "test";
        die();

        $container = $observer->getBlock();

        if(null !== $container && $container->getType() == 'adminhtml/sales_order_grid') {
            $data = array(
                'label'     => 'My button',
                'class'     => 'some-class',
                'onclick'   => 'setLocation(\' '  . Mage::getUrl('*/*', array('param' => 'value')) . '\')',
            );
            $container->addButton('my_button_identifier', $data);
        }

        return $this;
    }


}

Am I missing something or is there a better way to add a custom button to the admin interface of a module?

Any help would be greatly appreciated.

Thanks

1
  • You dont need to extend your observer from Varien_Event_Observer. Make sure you removed your cache. Also look for any log errors Commented Nov 14, 2016 at 11:40

2 Answers 2

1

First, an observer does not need to extend anything so you can replace:

class Ds_Imports_Model_Observer extends Varien_Event_Observer 

With:

class Ds_Imports_Model_Observer

Then you can also get rid of your __construct() method it's useless here.

Also, I suggest you hint the class parameter in your method by replacing:

public function addButtonTest($observer) {

With:

public function addButtonTest(Varien_Event_Observer $observer) {

But that's not the issue in your case you're mixing the area tags.

Magento allows three main area tags in the configuration:

  • adminhtml
  • frontend
  • global (which is basically the two above)

In your case, you've added the <adminhtml> area tag inside the <global> tag which is wrong.

Thus you need to fix your config.xml with the following code:

<?xml version="1.0"?>
<config>
    <modules>
        <Ds_Imports>
            <version>1.0.0</version>
        </Ds_Imports>
    </modules>

    <adminhtml>
        <events>
           <adminhtml_widget_container_html_before>
                <observers>
                    <add_button_test>
                        <type>singleton</type>
                        <class>Ds_Imports_Model_Observer</class>
                        <method>addButtonTest</method>
                    </add_button_test>
                </observers>
            </adminhtml_widget_container_html_before>
        </events>
        <layout>
            <updates>
                <ds_imports>
                    <file>importer.xml</file>
                </ds_imports>
            </updates>
        </layout>
    </adminhtml>

    <global>
        <helpers>
            <imports>
                <class>Ds_Imports_Helper</class>
            </imports>
        </helpers>
        <blocks>
            <imports>
                <class>Ds_Imports_Block</class>
            </imports>
            <!-- <adminhtml>
                <rewrite>
                    <sales_order_grid>Ds_Imports_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml> -->
        </blocks>
        <models>
            <imports>
                <class>Ds_Imports_Model</class>
                <resourceModel>imports_resource</resourceModel>
            </imports>
        </models>
        <resources>
            <imports_setup>
                <setup>
                    <module>Ds_Imports</module>
                </setup>
            </imports_setup>
        </resources>

    </global>
     <admin>
            <routers>
                <adminhtml>
                    <args>
                        <modules>
                            <ds_imports before="Mage_Adminhtml">Ds_Imports_Adminhtml</ds_imports>
                        </modules>
                    </args>
                </adminhtml>
            </routers>
        </admin>
</config>
0

I think you have problem with your config.xml file please change as below.

<?xml version="1.0"?>
<config>
 <modules>
    <Ds_Imports>
        <version>1.0.0</version>
    </Ds_Imports>
 </modules>

<global>
    <adminhtml>
        <layout>
            <updates>
                <ds_imports>
                    <file>importer.xml</file>
                </ds_imports>
            </updates>
        </layout>
    </adminhtml>
    <helpers>
        <imports>
            <class>Ds_Imports_Helper</class>
        </imports>
    </helpers>
    <blocks>
        <imports>
            <class>Ds_Imports_Block</class>
        </imports>
        <!-- <adminhtml>
            <rewrite>
                <sales_order_grid>Ds_Imports_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
            </rewrite>
        </adminhtml> -->
    </blocks>
    <models>
        <imports>
            <class>Ds_Imports_Model</class>
            <resourceModel>imports_resource</resourceModel>
        </imports>
    </models>
    <!--PUt your event here-->
    <events>
            <adminhtml_widget_container_html_before>
                <observers>
                    <add_button_test>
                        <type>singleton</type>
                        <class>imports/observer</class><!--change this line to-->
                        <method>addButtonTest</method>
                    </add_button_test>
                </observers>
            </adminhtml_widget_container_html_before>
    </events>
    <resources>
        <imports_setup>
            <setup>
                <module>Ds_Imports</module>
            </setup>
        </imports_setup>
    </resources>

</global>
 <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <ds_imports before="Mage_Adminhtml">Ds_Imports_Adminhtml</ds_imports>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.