Drupal Theme-Switching on Content

By default Drupal installs with a Site Wide Default theme. You may or may not set an Administrative theme which affects menu routes under /admin/*; if configured the Admin Theme can be configured to show on Node Edit forms as well.

I typically use a Default theme for public visitors and an Admin theme for the Admin area of Drupal. This creates a visual distinction between these 2 modes of interacting with Drupal. Most Admin themes also provide a single column admin theme — to maximize authoring space.

Recently I’ve been asked to switch the whole theme of a page’s content in the admin area to provide a preview of what a Student would see when accessing a site. Typically, I would recommend Themekey to accomplish this.

ThemeKey allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties. It can also be easily extended to support additional properties exposed by other modules.

But, there also exists a snippet of code to accomplish the same results for simple use-cases using a custom module and targeting specific menu router paths:

 * Implements hook_menu_alter().
 * Target a specific menu path and alter the theme callback.
function MYMODULE_menu_alter(&$items) {
  $items['admin/config/SOME-PAGE']['theme callback'] = 'MYMODULE_get_default_theme';

function MYMODULE_get_default_theme() {
  return variable_get('theme_default', 0);

The above code forces the Default Theme in Drupal to be used as the admin path. If you have an Admin Theme set — this overrides that setting, viola!

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

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