How to add a button into admin backend
In this tutorial I show how to add a button into admin backend. There are verious needs for this, the most common need is to add an additional function to a grid or detail site. I show you a general way to add buttons that can be used for catalog_product, cms_page_edit, sales_order or any other admin backend site.
How to add a button into admin backend
All this is done with a newly created module, so no core function is affacted. You can later use this module for any other shop so it is reuseable. The first step is to create a new event in your config.xml file:
1 2 3 4 5 6 7 8 9 10 11 12 | <global> <events> <adminhtml_widget_container_html_before> <observers> <yourmodulecode> <class>yourmodulecode/observer</class> <method>addNewButton</method> </yourmodulecode> </observers> </adminhtml_widget_container_html_before> </events> </global> |
The adminhtml_widget_container_html_before event is called every time before html code for a adminhtml (backend) site is generated. In this observer method we can make changes like adding a new button. Please replaceyourmodulecode with a unique code like your company name and module name. The event is called every time, so we need an if statement to check it type of the container is the right one (for example catalog_product). If you don’t know which typ you need here, just check the URL of you admin site. You find it right after index.php/admin/ (or any other word if you changed backend URL for security reasons).
Observer
In you config.xml you defined an event observer for adminhtml_widget_container_html_before that is computed in you addNewButton method. This method needs to be created in you Observer.php file. If you don’t have one, add it into you Model folder. Just be sure, that also you definition for this Model folder info config.xml like:
1 2 3 4 5 6 7 8 9 | <global> ... <models> <yourmodulecode> <class>Companyname_Modulecode_Model</class> </yourmodulecode> </models> ... </global> |
Please replace Companyname_Modulecode with you data. My Observer with Observer method looks as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php class Companyname_Modulecode_Model_Observer { public function addNewButton($observer) { $container = $observer->getBlock(); if (null !== $container && $container->getType() == 'adminhtml/catalog_product') { $data = array( 'label' => $this->__('My Button'), 'class' => 'save', 'onclick' => 'setLocation(\' ' . Mage::getUrl('*/*', array('param' => 'value')) . '\')', ); $container->addButton('my_new_button_identifier', $data); } return $this; } } |
The important part is the if. You have to check container type. For your product detail site it is catalog_product, for a CMS page it is cms_page_edit… For debug uses you may var_dump this type than you can read it from backend. Creating a new button is quite simple. You can define a label, a class an a onclick URL to compute this onclick action. Some possible classes for your new button:
- (no class)
- delete
- save
- go
Conclusion
To add a button to a adminhtml site, you only need to add an new event observer method. In this observer method you can change html for a backend site before it gets rendered. With an if statement you have to check if it is the correct site, because your observer method is called for every single site.