Drupal - Force a page to be rendered in admin theme (overlay mode)

You can also do this by making a custom module.

Method 1

Use this if you want to theme an existing page, based on Drupal's internal path (aka path source). This example uses hook_custom_theme.

<?php
function MYMODULE_custom_theme() {
  // match node/1
  if (arg(0) == 'node' && arg(1) == '1') {
    return variable_get('admin_theme');
  }
}

Method 2

Use this if you want to theme an existing page, based on URL path (aka path alias). This example also uses hook_custom_theme.

<?php
function MYMODULE_custom_theme() {
  // get arguments
  $arg = explode('/', substr(request_uri(), strlen(base_path())));
  // match {wildcard}/path 
  // Using strpos as $arg[1] may end up having stuff like so ?order=title&sort=asc
  if (isset($arg[1]) && strpos($arg[1], 'path') !== false && !isset($arg[2])) {
    return variable_get('admin_theme');
  }
}

Method 3

Use this if you want to theme and create a page. This example uses hook_menu. To learn more, take a look at another great article on hook_menu.

<?php
function MYMODULE_menu() {
  $items = array();

  // match some/path
  $output['some/path'] = array(
    'title' => t('Page Title'),
    'page callback' => 'MYMODULE_page',
    'theme callback' => 'variable_get',
    'theme arguments' => array('admin_theme'),
  )
}

function MYMODULE_page() {
  return 'Hello world.';
}

If you are a module developer, you can use hook_admin_paths to define which paths are to be rendered with the administration theme.


There is a contrib module ThemeKey that

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. In combination with Drupal's theme inheritance and ThemeKey Properties you can easily achieve features like:

individually-styled channels a front-page / "splash" screen a date/time-selected Christmas theme mobile themes for different auto-detected mobile devices special themes for "limited" or "old" browsers content, user, or role -specific themes indicating your environment (production, staging, testing, sandbox, … ) testing your redesign safely on a live server

Check also related question here.

Tags:

Theming

7