How to override Magento admin view (template) files, quick and dirty way

Featured Image

Suppose you are working on a extension that needs to either change some stuff or implement new stuff in the one of the Magento admin areas. For example, imagine a “Images” tab on the product edit section. If you were in a position where you need to do certain modifications on “Images” tab then you would most likely need to modify the app/design/adminhtml/default/default/template/catalog/product/helper/gallery.phtml file to do so. One of Inchoo coworkers and a good friend of mine, Ivan Weiler, made a Magento Admin theme module that enables you to achieve similar functionality for admin template files as you have for frontend template files.

However, sometimes you wont wish your extensions depend on the additional module for certain functionality. Stuff like “overriding” the admin view files can easily be achieved with just a few minor modifications of extended class.

Content of my app\code\community\MyCompany\MyModule\Block\Adminhtml\Catalog\Product\Helper\Form\Gallery\Content.php file.

class MyCompany_MyModule_Block_Adminhtml_Catalog_Product_Helper_Form_Gallery_Content extends Mage_Adminhtml_Block_Catalog_Product_Helper_Form_Gallery_Content
{
    /**
     * This method has been overridden merely for the purpose of setting up a new view file
     * to be used in place of the default theme folder.
     *
     * @see app/code/core/Mage/Core/Block/Mage_Core_Block_Template#fetchView($fileName)
     */
    public function fetchView($fileName)
    {
        extract ($this->_viewVars);
        $do = $this->getDirectOutput();
        if (!$do) { ob_start(); }
        include getcwd().'/app/code/community/MyCompany/MyModule/blocks/Adminhtml/catalog/product/helper/gallery.phtml';
        if (!$do) {$html = ob_get_clean(); }
        else { $html = ''; }
        return $html;
    }
}

Which goes inline with the config.xml file entry like:

...
<blocks>
	<adminhtml>
		<rewrite>
			<catalog_product_helper_form_gallery_content>MyCompany_MyModule_Block_Adminhtml_Catalog_Product_Helper_Form_Gallery_Content</catalog_product_helper_form_gallery_content>
		</rewrite>
	</adminhtml>
</blocks>
...

In the example below, I am pointing the block to look for my view file within my module folder, and not within some Magento design folder. Basically my gallery.phtml is the same as the original Magento adminhtml view file for the same purpose, but now I can go in and implement my additional stuff in it.

Approach like this can have its advantage in terms of keeping all the required overridden design files in your own module directory.

For those who need it… cheers.

14
Top

Care to rate this post?

Author

Branko Ajzele

Ex Inchooer

Worked at Inchoo as a Backend Developer/CTO from 2008 to 2013

Other posts from this author

Discussion 14 Comments

Add Comment
  1. Anatoly A. Kazantsev

    What about this lines in your config.xml:

    default

    yout_theme_name

    Then you simple do the same as with front end themes.

  2. Very nice. I like this fetchView method better since the config.xml method can only be used by one module! If you install two modules that use that method they will conflict, so bravo for finding a friendlier solution, although I would still put my template files in the adminhtml/default/default/template// folder.

  3. Anatoly A. Kazantsev

    This approach doesn’t solve conflict problems between modules.

    Using this method two modules can overwrite same class, so you get same problem on another level.

    The difference is how often conflicts happen.

  4. Exactly, but it is potentially a very large difference.

  5. Rubin

    Does anyone know how to override adminhtml dashboard graph block ? I tried with this code and doesn’t work. However, this code works perfectly with other dashboard blocks. I don’t understand why doesn’t work with only graph.

    Mycompany_Mymodule_Block_Graph

  6. arpit chugh

    can any one tell me where is that gallery .phtml

  7. Hello,

    I’m trying to override the adminhtml widget grid, since this is something i have to overwrite often and would prefer not to modify core files. :-)

    I think I’ve done everything correctly, but could really use an extra set of eyes, because it just isn’t working. When it works, it will expand the number of items that can be viewed at once in the admin.

    You can download and see my very simple code here:
    http://redesigned.com/test_module_001.zip

    Please let me know if I’m doing something wrong or if you are able to get this to work.

    I’d greatly appreciate any help you can offer.

    Thanks in advance!

    Kind Regards,
    Josh

  8. Hi,

    can anyone tell me how to override frontend cart.phtml file using custom module.

    Thanks

  9. Gershon Herczeg

    You just helped me with a extension i am working on thanks bro

  10. Hi,
    I need to customize the ‘Prices’ tab of products in admin. I don’t know how to edit the tab. Is there a .phtml file for that. Please respond to the query. It is urgent.

  11. Henrique

    Greate Post!

    Exist other form to get file?

    include getcwd().’/app/code/community/MyCompany/MyModule/blocks/Adminhtml/catalog/product/helper/gallery.phtml’;

    Thanks man!!

  12. What about this solution? I think it’s a MUCH cleaner way and not necessarily longer:

    http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/admin/changing_the_adminhtml_template

  13. Cool resource! Thanks for creating it.
    It save my lot time.

  14. Snehal

    Nice Blog..

    I have one query regarding this. If I want to overwrite options.phtml file thn how can I do so.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top