0

I'm new to Magento and I'm trying to add an additional button to a pre-existing admin form using an observer. I set up my config to what I believe is the right event handle. I created an observer and added the name and method to config. I don't believe my observer is ever hit since Mage log is not printing. Can anyone see where I'm going wrong?

Sales.xml - (Enterprise layout file)

<layout>
   <adminhtml_sales_order_view>
        <reference name="content">
            <block type="adminhtml/sales_order_view" name="sales_order_edit"></block>
        </reference>
   </adminhtml_sales_order_view>
</layout>

config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
        <Mycompany_MyModule>
            <version>0.1.0</version>
        </Mycompany_MyModule>
    </modules>
    <global>
        <events>
            <adminhtml_sales_order_view>
                <observers>
                    <my_observer>
                        <type>singleton</type>
                        <class>Mycompany_MyModule_Model_Observer</class>
                        <method>addMyButton</method>
                    </my_observer>
                </observers>
            </adminhtml_sales_order_view>
        </events>
    </global>
</config>

Observer.php

<?php

class Mycompany_MyModule_Model_Observer
{
    public function addMyButton($observer)
    {
        Mage::log('test observer');

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

4 Answers 4

0

You need to extend layout instead of writing observer. You cannot dispatch a new event using layout files. Please check more on how to extend layout handles

1
  • Right, I see. I was kind of merging the layout update and observer ideas together. I will check this out now. Thanks. Commented Aug 12, 2016 at 4:15
0

There is no event with the name <adminhtml_sales_order_view>, it is a layout handle.
You need to use event like <core_block_abstract_prepare_layout_before> as below :

<adminhtml>
        <events>
            <core_block_abstract_prepare_layout_before>
                <observers>
                    .....
                </observers>
            </core_block_abstract_prepare_layout_before>
        </events>
    </adminhtml> 

Reference Links :

1
  • Thank you for pointing this out. I can't tell if there's a good event that would allow me to append to this admin form. I'm going to checkout layout update, but appreciate your explanation for clarity. Commented Aug 12, 2016 at 4:18
0

FINAL CODE - I did a layout update and it worked for me.

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MyCompany_MyModule>
            <version>0.1.0</version>
        </MyCompany_MyModule>
    </modules>
 <adminhtml>
     <layout>
          <updates>
               <myupdate>
                     <file>myupdate.xml</file>
               </myupdate>
          </updates>
     </layout>
 </adminhtml>
</config>

myupdate.xml

<layout version="0.1.0">
    <adminhtml_sales_order_view>
        <reference name="sales_order_edit">
            <action method="addButton">
                <id>my_button_identifier</id>
                <data>
                    <label>My button</label>
                    <class>go</class>
                </data>
            </action>
        </reference>
    </adminhtml_sales_order_view>
</layout>
0

Have you defined your <blocks> and <models> within config.xml? If you didnt define models, that will explain why your observer didn't work. Well, that, and the fact that you have a layout handle as your event.

Additionally, instead of

if(null !== $container && $container->getType() == 'adminhtml/sales_order_view')

you should go with

if($container instanceof Mage_Adminhtml_Block_Sales_Order_View)

That's because if you have some module or customization in the future, the "type" may change but it will most likely still extend from the original class.

Additionally, a super helpful tool created by Alan Storm, Commerce Bug. I use it every single day. In fact, it will provide you with a list of available observer events available on a given page.

Also, xdebug is very helpful when debugging Magento. You will know right away if your observer is firing. I made a short video on how to install xdebug. Xdebug Installation

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.