Drupal允许为管理后台设置独立的theme,保存在系统变量variable_get('admin_theme')。

Drupal使用全局变量$theme来保存当前请求对应的主题。Drupal在启动时初始化$theme变量:

function _drupal_bootstrap_full() {
... ...
menu_set_custom_theme();
drupal_theme_initialize();
module_invoke_all('init');
} function menu_set_custom_theme() {
menu_get_custom_theme(TRUE);
} function menu_get_custom_theme($initialize = FALSE) {
$custom_theme = &drupal_static(__FUNCTION__);
// Skip this if the site is offline or being installed or updated, since the
// menu system may not be correctly initialized then.
if ($initialize && !_menu_site_is_offline(TRUE) && (!defined('MAINTENANCE_MODE') || (MAINTENANCE_MODE != 'update' && MAINTENANCE_MODE != 'install'))) {
// First allow modules to dynamically set a custom theme for the current
// page. Since we can only have one, the last module to return a valid
// theme takes precedence.
// 调用hook_custom_theme()获取自定义theme
$custom_themes = array_filter(module_invoke_all('custom_theme'), 'drupal_theme_access');
if (!empty($custom_themes)) {
$custom_theme = array_pop($custom_themes);
}
// If there is a theme callback function for the current page, execute it.
// If this returns a valid theme, it will override any theme that was set
// by a hook_custom_theme() implementation above.
// 在hook_menu()定义时也可以用theme callback/theme arguments定义theme
$router_item = menu_get_item();
if (!empty($router_item['access']) && !empty($router_item['theme_callback']) && function_exists($router_item['theme_callback'])) {
$theme_name = call_user_func_array($router_item['theme_callback'], $router_item['theme_arguments']);
if (drupal_theme_access($theme_name)) {
$custom_theme = $theme_name;
}
}
}
return $custom_theme;
} function drupal_theme_initialize() {
global $theme, $user, $theme_key; // If $theme is already set, assume the others are set, too, and do nothing
if (isset($theme)) {
return;
} drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
$themes = list_themes(); // Only select the user selected theme if it is available in the
// list of themes that can be accessed.
// 检查有没有设置用户主题?
$theme = !empty($user->theme) && drupal_theme_access($user->theme) ? $user->theme : variable_get('theme_default', 'bartik'); // Allow modules to override the theme. Validation has already been performed
// inside menu_get_custom_theme(), so we do not need to check it again here.
// 检查有没有设置自定义主题?
$custom_theme = menu_get_custom_theme();
$theme = !empty($custom_theme) ? $custom_theme : $theme; ... ...
}

system模块实现了hook_custom_theme()钩子,检查当前请求menu是否属于admin:

function system_custom_theme() {
if (user_access('view the administration theme') && path_is_admin(current_path())) {
return variable_get('admin_theme');
}
} function path_is_admin($path) {
$path_map = &drupal_static(__FUNCTION__);
if (!isset($path_map['admin'][$path])) {
$patterns = path_get_admin_paths();
$path_map['admin'][$path] = drupal_match_path($path, $patterns['admin']);
$path_map['non_admin'][$path] = drupal_match_path($path, $patterns['non_admin']);
}
return $path_map['admin'][$path] && !$path_map['non_admin'][$path];
}

system_custom_theme()调用path_is_admin()检查请求是否属于admin。path_is_admin()再调用path_get_admin_paths()返回哪些请求是admin,哪些请求是non_admin。

function path_get_admin_paths() {
$patterns = &drupal_static(__FUNCTION__);
if (!isset($patterns)) {
$paths = module_invoke_all('admin_paths');
drupal_alter('admin_paths', $paths);
// Combine all admin paths into one array, and likewise for non-admin paths,
// for easier handling.
$patterns = array();
$patterns['admin'] = array();
$patterns['non_admin'] = array();
foreach ($paths as $path => $enabled) {
if ($enabled) {
$patterns['admin'][] = $path;
}
else {
$patterns['non_admin'][] = $path;
}
}
$patterns['admin'] = implode("\n", $patterns['admin']);
$patterns['non_admin'] = implode("\n", $patterns['non_admin']);
}
return $patterns;
}

path_get_admin_paths()如何判断请求是admin还是non_admin?这又要调用另外一个钩子hook_admin_paths()。system模块就实现了这个钩子:

function system_admin_paths() {
$paths = array(
'admin' => TRUE,
'admin/*' => TRUE,
'batch' => TRUE,
// This page should not be treated as administrative since it outputs its
// own content (outside of any administration theme).
'admin/reports/status/php' => FALSE,
);
return $paths;
}

总结一下:
1. system模块通过实现hook_admin_paths()钩子定义了所有以admin/开头的请求都属于admin请求。
2. system模块通过实现hook_custom_theme()钩子定义了所有admin请求都使用admin_theme。

Drupal administration theme的更多相关文章

  1. 使用Drush管理Drupal站点

    Drush(Drush = Drupal + Shell)就是使用命令行命令来操作Drupal站点,它的命令格式与git类似,都是双字命令(drush + 实际的命令).既然是命令行命令,也就可以使用 ...

  2. 【转】为drupal初学者准备的12个精品课程

    下面是一些网上免费的drupal教程,这些教程将对初学者和那些从别的CMS转向drupal的开发者非常有帮助.初级教程 1.在开始用drupal之前,你要知道一些基本的东西,内容很简单,但有些还是值得 ...

  3. Drupal中hook_theme函数用法

    在开发的时候不免要使用到drupal theme 定义.举个简单的例子: 复制代码 代码如下: <?phpfunction modulename_theme() { //开始定义自己的theme ...

  4. ThinkPHP 3.2.3心得

    个人还是蛮喜欢tp的比其他的php框架轻(只接触过drupal.tp),而且上手容易(struts这种action的方式,对于java程序员来说).目录结构也比较简单易懂,提供的一些函数也比较实用.对 ...

  5. drupal THEME主要文件

    **.info 文件** .info 文件是一个必需的文件:Drupal 必须包括它,才干看到主题. .info 文件告诉 Drupal 主题的内部名称.比如,假设这个文件的名称是 ibmtheme. ...

  6. drupal module 自定义

    01 <?php function mytracer_menu() { $items = array(); $items['admin/config/mytracer'] = array( 't ...

  7. 在CentOS 7下试验Drupal 7

    按顺序安装好Apache.MariaDB和PHP,启动Apache和MariaDB,创建一个UTF-8字符集的数据库. > create database if not exists drupa ...

  8. Drupal常用的模块

    CCK (Content Construction Kit ) : 添加字段模块 Views:生成列表 Tinymce:(Wysiwyg Editor) 常用的编辑器之一 Ajax Form Buil ...

  9. drupal基本知识介绍

    2. Drupal 安装在安装Drupal前,你需要在服务器上先搭建一个PHP+MySQL环境.专业网站一般是安装LAMP(Linux+Apache+MySQL+PHP).环境的搭建可参考如下文章:  ...

随机推荐

  1. .NET:CLR via C# Assembly Loading

    基础知识 Internally, the CLR attempts to load this assembly by using the System.Reflection.Assembly clas ...

  2. 图文详解 Android Binder跨进程通信机制 原理

    图文详解 Android Binder跨进程通信机制 原理 目录 目录 1. Binder到底是什么? 中文即 粘合剂,意思为粘合了两个不同的进程 网上有很多对Binder的定义,但都说不清楚:Bin ...

  3. Jersey 框架取到所有参数的方法

    /**  * 测试post取参数  *   * @return  */ @POST @Consumes("application/x-www-form-urlencoded") p ...

  4. 如何解决Maven速度慢

    注:oschina已失效 Maven 远程仓库 <mirror> <id>ui</id> <mirrorOf>central</mirrorOf& ...

  5. fisher精确检验(fisher’s exat test)和超几何分布

  6. JavaBean示例

    例1.通过非可视化的JavaBean,封装邮箱地址对象,通过JSP页面调用该对象来验证邮箱地址是否合法. (1)创建名称为Email的JavaBean对象,用于封装邮箱地址,关键代码如下: packa ...

  7. BULLET物理DEMO最新版本

    鼠标右键按下并拖动         旋转视角WSAD                         前后左右RF                             上下QE           ...

  8. Same Tree leetcode java

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  9. Word Break II leetcode java

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  10. Swap Nodes in Pairs leetcode java

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...