Setting custom Admin theme from a Module

Last modified by resonance on Fri, June 25, 2010 07:10
Source|Old Revisions  

This is an old revision of the document!

Frontend templates are simple to change via Admin, from default/default to default/mytheme. The Admin templates take a little more work, but it’s easy to do. This method was tested under Magento

These steps apply within a Magento module. There are many tutorials on creating a basic module, so we will not repeat it here. All you need are the basic module directories, the module’s xml file under /app/etc/modules, and a skeleton config.xml for your module.

In the directions below, replace mycompany and mymodule with values appropriate to your module.

Create your Admin Theme

Create a directory /app/design/adminhtml/default/mytheme

In that directory, put your template, layout, and locale directories, and copy in the templates and other files you wish to override from /app/design/adminhtml/default/default. This works the same as changing a theme for the frontend.

Edit config.xml

We will add an entry in your module’s config.xml file to add an observer to the adminhtml_controller_action_predispatch_start event. This observer calls the preDispatch method that we define in our observer class, Mycompany_Mymodule_Model_Adminhtmlobserver.

Add this code in the <global> section of your module’s config.xml:

  1. <events>
  2.     <!-- Name of the event to observe: -->
  3.     <adminhtml_controller_action_predispatch_start>
  4.         <observers>
  5.             <!-- A unique label for your observer: -->
  6.             <!-- By convention, this matches the class name -->
  7.             <mycompany_mymodule_model_adminhtmlobserver>
  8.                 <type>singleton</type>
  9.                 <!-- Our observer's class name: -->
  10.                 <!-- found in file /app/code/local/Mycompany/Mymodule/Model/Adminhtmlobserver.php -->
  11.                 <class>Mycompany_Mymodule_Model_Adminhtmlobserver</class>
  12.                 <!-- The method in our class to call: -->
  13.                 <method>preDispatch</method>
  14.             </mycompany_mymodule_model_adminhtmlobserver>
  15.         </observers>
  16.     </adminhtml_controller_action_predispatch_start>
  17. </events>

Create an Event Observer

Create a file /app/code/local/MyCompany/MyModule/Model/Adminhtmlobserver.php

In that file place this code:

  1. <?php
  3. class Ntl_Sms_Model_Adminhtmlobserver {
  4.     // set the theme for the admin backend
  5.     public function preDispatch($observer) {
  6.         Mage::getDesign()->setArea('adminhtml')
  7.                         ->setPackageName('mytheme')
  8.                         ->setTheme('mytheme');
  9.     }
  10. }

That’s all it takes!