13

Is there a way to force a page to be rendered in admin theme, Like pages that their path begins with 'admin/'?

5 Answers 5

4

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.

14

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.';
}
7

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

4

Simply give the page a path beginning with admin: 'admin/mypage'

5
  • 1
    Why the downvote? This functionality is in D7 core, the question does not specify which version, but at time of posters question D7 was released. Commented Jul 30, 2014 at 19:56
  • Yep, perfectly sensefull comment IMO.
    – Countzero
    Commented Oct 27, 2014 at 9:00
  • 2
    it doesn't seem that custom pages beginning with 'admin/' necessarily load the admin theme.
    – DrCord
    Commented Mar 3, 2015 at 21:50
  • perfect and simplistic for views! Commented Sep 28, 2016 at 16:07
  • This does not work.
    – Christia
    Commented May 2, 2017 at 8:07
1
  1. This functionality is now in D7 core.

  2. There's a module that effectively exposes "hook_admin_paths" to the UI: https://drupal.org/project/admin_theme

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.