Programmatically add a custom block in Magento Admin

Programmatically add a custom block in Magento Admin

Howdy! Recently I was in doubt about how to add custom Magento Block for my client’s requirements. One approach was to deal with it through .xml file, but when I was thinking about it I asked myself: “ Why add another .xml and load it?” And at this point, I decided to go with programmatic method.

Yeah, right! What is it all about?  It is much easier to understand it than it may be to spell it. In this article we will cover complete section in deep and here you can download complete source code from my Github repository. We’ve got a lot of code to cover, so let’s get started!

Always, but always, my first point when I am developing custom module or examining existing one is config.xml ( of course this assumes that you set your “module registration file”  under  app/etc/modules/Inchoo_Adminblock.xml. After we set all of that we are going to create folder structure. And here we go with config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Inchoo_AdminBlock>
<version>1.0.0.0</version>
</Inchoo_AdminBlock>
</modules>
<global>
<blocks>
<inchoo_adminblock>
<class>Inchoo_AdminBlock_Block</class>
</inchoo_adminblock>
</blocks>
</global>
<admin>
<routers>
<adminhtml>
<args>
<modules>
<inchoo_adminblock after="Mage_Adminhtml">Inchoo_Adminblock_Adminhtml</inchoo_adminblock>
</modules>
</args>
</adminhtml>
</routers>
</admin>
</config>

Here we define our block class which we can use for further development. Above that inside admin tags we defined our routers which will handle our URL and our route to the controller class. I use attribute “after” and that is where I am telling Magento: ” Hey partner! Can you load this to me after “Mage_Adminhtml“. With this approach Magento will always load its own controllers even in case that we called ours with the same name and put it in same structure, then it will call inchoo_adminblock.

Admin Block FIle Structure
I will not go in deep how to create controller and block but you have complete article files on my Github so you can download it. Ok! What’s the next thing we need to do? We will create our .phtml block under app/design/adminhtml/default/default/inchoo/adminblock.phtml and inside it you can put your html.
Now we are going to do some magic and call that block programmatically. Go inside controller and type the following:

<?php
 
<?php
 
class Inchoo_Adminblock_Adminhtml_CreateController extends Mage_Adminhtml_Controller_Action{
 
public function indexAction(){
$this->loadLayout()
->_addContent(
$this->getLayout()
->createBlock('inchoo_adminblock/adminhtml_adminblock')
->setTemplate('inchoo/adminblock.phtml'))
->renderLayout();
}
}

Here we created our block which we defined in .config.xml and now we can put in it every logic or custom php method / parameters  and call them in our template. Isn’t that nice and simple? We can easily test it with this URL: yourvirtualhost/admin/create/index/ and you can see that the job is done!

Job Done
Hope you will find this useful! Happy coding!

You made it all the way down here so you must have enjoyed this post! You may also like:

Add Pagination to Custom Collection in Magento Dario Trbovic
Dario Trbovic, | 7

Add Pagination to Custom Collection in Magento

Layout of your custom controller in Magento? Vedran Subotic
Vedran Subotic, | 12

Layout of your custom controller in Magento?

Programatically create Magento blocks and inject them into layout Branko Ajzele
Branko Ajzele, | 35

Programatically create Magento blocks and inject them into layout

4 comments

  1. Solved issue by myself maybe helpful- disable secret key in magento admin
    Stores => Configuration => Advanced => Admin => Security => Add Secret Key to URLs

  2. When I add the code it is not showing the menu in the URL, when I browse the page, it redirects to dashboard page.

  3. “Why add another .xml and load it?”

    Because…

    1. You want to follow the Separation of Concerns principle. If you follow the MVC pattern the Controller (C) shouldn’t care about how to build the View (V). The layout XML is the right place for this work.

    2. It is much easier to remove the block, change the template etc. in another extension if necessary. If you use the controller, somebody has to rewrite the complete controller action just to change the view (again: Separation of Concerns). You just have more flexibility.

    3. Magento combines all the layout XML files and caches the result away. No need to worry about that one more XML file.

    And I’d say there are more reasons for using layout XML but these are the first three ones coming from the top of my head. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.