You are here

admin_theme.module in Administration theme 5

Same filename and directory in other branches
  1. 8 admin_theme.module
  2. 6 admin_theme.module
  3. 7 admin_theme.module

Enable the administration theme on more pages then possible with Drupal's default administration page.

File

admin_theme.module
View source
<?php

/**
 * @file
 * Enable the administration theme on more pages then possible with Drupal's default administration page.
 */

/**
 * Implementation of hook_perm().
 */
function admin_theme_perm() {
  return array(
    'access admin theme',
  );
}

/**
 * Get the variable name for a certain option.
 *
 * @param $module
 *   String. Module that defines this option.
 * @param $params
 *   String. Name of the option.
 * @return
 *   String. Variable name for the option.
 */
function admin_theme_variable_name($module, $option) {
  return 'admin_theme_' . $module . '_' . $option;
}

/**
 * Get all module defined options.
 *
 * @return
 *   Array. All options.
 */
function admin_theme_list() {
  $options = array();
  foreach (module_list() as $module) {
    $module_options = module_invoke($module, 'admin_theme_options', 'info');
    if (count($module_options) > 0) {
      foreach ($module_options as $option => $info) {
        $info['option'] = $option;
        $info['module'] = $module;
        $options[] = $info;
      }
    }
  }
  return $options;
}

/**
 * Implementation of hook_form_alter().
 */
function admin_theme_form_alter($form_id, &$form) {
  if ($form_id == 'system_admin_theme_settings') {

    // we want our checkboxes before the submit button
    $form['buttons']['#weight'] = 10;

    // define a fieldset for the theme
    $form['theme'] = array(
      '#type' => 'fieldset',
      '#title' => t('Theme'),
      '#collapsible' => TRUE,
    );
    $form['theme']['#description'] = $form['admin_theme']['#description'];
    unset($form['admin_theme']['#description']);

    // add the core administration theme select field to this fieldset
    $form['theme']['admin_theme'] = $form['admin_theme'];
    unset($form['admin_theme']);

    // define a fieldset for the page selection
    $form['pages'] = array(
      '#type' => 'fieldset',
      '#title' => t('Pages'),
      '#collapsible' => TRUE,
      '#description' => t('Choose which pages should be displayed with the administration theme.'),
    );

    // add all options as checkboxes to the admin theme settings form
    $list = admin_theme_list();
    foreach ($list as $info) {
      $var = admin_theme_variable_name($info['module'], $info['option']);
      $form['pages'][$var] = array(
        '#type' => 'checkbox',
        '#title' => array_key_exists('title', $info) ? $info['title'] : NULL,
        '#description' => array_key_exists('description', $info) ? $info['description'] : NULL,
        '#default_value' => variable_get($var, '0'),
      );
    }

    // allow the user to define a set of pages where the admin theme should or should not be applied to
    $form['pages']['custom'] = array(
      '#type' => 'fieldset',
      '#title' => t('Custom pages'),
      '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
        '%blog' => 'blog',
        '%blog-wildcard' => 'blog/*',
        '%front' => '<front>',
      )),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 9,
    );
    $form['pages']['custom']['admin_theme_path'] = array(
      '#type' => 'textarea',
      '#title' => t('Use administration theme on the following pages'),
      '#default_value' => variable_get('admin_theme_path', ''),
    );
    $form['pages']['custom']['admin_theme_path_disallow'] = array(
      '#type' => 'textarea',
      '#title' => t('Do not use administration theme on the following pages'),
      '#description' => t('If a path appears here, the administration theme is not shown even if all above options apply.'),
      '#default_value' => variable_get('admin_theme_path_disallow', ''),
    );
  }
}

/**
 * Implementation of hook_init().
 */
function admin_theme_menu($may_cache) {
  if (!$may_cache) {
    $admin_theme_disallow = FALSE;
    $admin_theme = FALSE;

    // check if some paths are disallow to get the theme
    if (trim(variable_get('admin_theme_path_disallow', '')) != '') {

      // pages that are defined by their normal path
      $admin_theme_disallow = _admin_theme_drupal_match_path($_GET['q'], variable_get('admin_theme_path_disallow', ''));

      // pages that are defined with their alias
      $alias = drupal_get_path_alias($_GET['q']);
      if ($alias != $_GET['q']) {
        $admin_theme_disallow = $admin_theme || _admin_theme_drupal_match_path($alias, variable_get('admin_theme_path_disallow', ''));
      }
    }

    // we should not show the admin theme if the user has no access or the path is in the disallow list
    if (!user_access('access admin theme') || $admin_theme_disallow) {
      global $custom_theme;
      unset($_GLOBALS['custom_theme']);
      return;
    }

    // check if an option is enabled and if it results to TRUE
    $list = admin_theme_list();
    foreach ($list as $info) {
      $var = admin_theme_variable_name($info['module'], $info['option']);
      if ((bool) variable_get($var, '0') && module_invoke($info['module'], 'admin_theme_options', 'check', $info['option'])) {
        $admin_theme = TRUE;
      }
    }

    // some custom defined pages should get admin theme
    if (trim(variable_get('admin_theme_path', '')) != '') {

      // pages that are defined by their normal path
      $admin_theme = $admin_theme || _admin_theme_drupal_match_path($_GET['q'], variable_get('admin_theme_path', ''));

      // pages that are defined with their alias
      $alias = drupal_get_path_alias($_GET['q']);
      if ($alias != $_GET['q']) {
        $admin_theme = $admin_theme || _admin_theme_drupal_match_path($alias, variable_get('admin_theme_path', ''));
      }
    }

    // Use the admin theme for the current request (if global admin theme setting is checked).
    if ($admin_theme) {
      global $custom_theme;
      $custom_theme = variable_get('admin_theme', '0');
      drupal_add_css(drupal_get_path('module', 'system') . '/admin.css', 'module');
    }
  }
}

/**
 * Implementation of hook_admin_theme().
 */
function admin_theme_admin_theme_options($op = 'info', $option = NULL) {
  switch ($op) {
    case 'info':
      $options = array();
      $options['node'] = array(
        'title' => t('Content editing'),
        'description' => t('Use the administration theme when editing existing posts or creating new ones.'),
      );
      if (module_exists('img_assist')) {
        $options['img_assist'] = array(
          'title' => t('Image assist'),
          'description' => t('Use the administration theme when viewing the Image assist popup window.'),
        );
      }
      if (module_exists('coder')) {
        $options['coder'] = array(
          'title' => t('Code reviews'),
          'description' => t('Use the administration theme when viewing Coder code reviews.'),
        );
      }
      if (module_exists('devel')) {
        $options['devel'] = array(
          'title' => t('Devel pages'),
          'description' => t('Use the administration theme when viewing pages of the devel module (hook_elements(), Dev render, Dev load, Session viewer, Theme registery, Variable editor, ...).'),
        );
      }
      if (module_exists('webform')) {
        $options['webform_results'] = array(
          'title' => t('Webform submissions.'),
          'description' => t('Use the administration theme when viewing webform submissions.'),
        );
      }
      if (module_exists('statistics')) {
        $options['statistics'] = array(
          'title' => t('Statistics pages'),
          'description' => t('Use the administration theme when viewing pages of the statistics module.'),
        );
      }
      return $options;
    case 'check':
      switch ($option) {
        case 'node':
          return arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit');
        case 'img_assist':
          return arg(0) == 'img_assist';
        case 'coder':
          return arg(0) == 'coder';
        case 'devel':
          return arg(0) == 'devel' || arg(0) == 'node' && arg(2) == 'devel';
        case 'webform_results':
          return arg(0) == 'node' && arg(2) == 'results';
        case 'statistics':
          return (arg(0) == 'node' || arg(0) == 'user') && arg(2) == 'track';
      }
  }
}

/**
 * Check if a path matches any pattern in a set of patterns.
 * Note: This is a port of the Drupal 6 function drupal_match_path($path, $patterns).
 *
 * @param $path
 *   The path to match.
 * @param $patterns
 *   String containing a set of patterns separated by \n, \r or \r\n.
 *
 * @return
 *   Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
 */
function _admin_theme_drupal_match_path($path, $patterns) {
  static $regexps;
  if (!isset($regexps[$patterns])) {
    $regexps[$patterns] = '/^(' . preg_replace(array(
      '/(\\r\\n?|\\n)/',
      '/\\\\\\*/',
      '/(^|\\|)\\\\<front\\\\>($|\\|)/',
    ), array(
      '|',
      '.*',
      '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
    ), preg_quote($patterns, '/')) . ')$/';
  }
  return preg_match($regexps[$patterns], $path);
}

Functions

Namesort descending Description
admin_theme_admin_theme_options Implementation of hook_admin_theme().
admin_theme_form_alter Implementation of hook_form_alter().
admin_theme_list Get all module defined options.
admin_theme_menu Implementation of hook_init().
admin_theme_perm Implementation of hook_perm().
admin_theme_variable_name Get the variable name for a certain option.
_admin_theme_drupal_match_path Check if a path matches any pattern in a set of patterns. Note: This is a port of the Drupal 6 function drupal_match_path($path, $patterns).