Drupal的默认主题bartik会在页面顶部显示系统信息,例如警告、状态等。这个过程是如何实现的?
首先,在bartik目录下找到page.tpl.php,这是bartik主题的页面显示模板。其中有一段是处理$messages的:
<?php if ($messages): ?>
<div id="messages"><div class="section clearfix">
<?php print $messages; ?>
</div></div> <!-- /.section, /#messages -->
<?php endif; ?>
这里的$messages变量从哪里来的呢?在includes/theme.inc文件中找到system_process_page()函数,这是对page hook的处理函数,调用了theme('status_messages')输出系统信息HTML。
function template_process_page(&$variables) {
... ... // Generate messages last in order to capture as many as possible for the
// current page.
if (!isset($variables['messages'])) {
$variables['messages'] = $variables['show_messages'] ? theme('status_messages') : '';
}
}
status_messages是另外一个theme hook,在system模块的hook_theme钩子中定义:
function system_theme() {
return array_merge(drupal_common_theme(), array(... ...});
} function drupal_common_theme() {
return array(
'status_messages' => array(
'variables' => array('display' => NULL),
),
};
}
status_messages hook是用theme_status_messages()函数输出的,在theme.inc里面:
function theme_status_messages($variables) {
$display = $variables['display'];
$output = ''; $status_heading = array(
'status' => t('Status message'),
'error' => t('Error message'),
'warning' => t('Warning message'),
);
foreach (drupal_get_messages($display) as $type => $messages) {
$output .= "<div class=\"messages $type\">\n";
if (!empty($status_heading[$type])) {
$output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
}
if (count($messages) > 1) {
$output .= " <ul>\n";
foreach ($messages as $message) {
$output .= ' <li>' . $message . "</li>\n";
}
$output .= " </ul>\n";
}
else {
$output .= $messages[0];
}
$output .= "</div>\n";
}
return $output;
}
theme_status_messages()函数调用drupal_get_message()获取系统信息,与此对应地设置系统信息的函数是drupal_set_message()。
function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
if ($message) {
if (!isset($_SESSION['messages'][$type])) {
$_SESSION['messages'][$type] = array();
} if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
$_SESSION['messages'][$type][] = $message;
} // Mark this page as being uncacheable.
drupal_page_is_cacheable(FALSE);
} // Messages not set when DB connection fails.
return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
} function drupal_get_messages($type = NULL, $clear_queue = TRUE) {
if ($messages = drupal_set_message()) {
if ($type) {
if ($clear_queue) {
unset($_SESSION['messages'][$type]);
}
if (isset($messages[$type])) {
return array($type => $messages[$type]);
}
}
else {
if ($clear_queue) {
unset($_SESSION['messages']);
}
return $messages;
}
}
return array();
}
从theme_status_messages()可知,系统信息有三类:status、error、warning。每一类系统信息对应一个css样式,在system.messages.css中定义。换句话说,不同类别的系统信息,显示的图标和字体都在system.messages.css中控制。
div.messages {
background-position: 8px 8px; /* LTR */
background-repeat: no-repeat;
border: 1px solid;
margin: 6px 0;
padding: 10px 10px 10px 50px; /* LTR */
} div.status {
background-image: url(../../misc/message-24-ok.png);
border-color: #be7;
} div.warning {
background-image: url(../../misc/message-24-warning.png);
border-color: #ed5;
} div.error {
background-image: url(../../misc/message-24-error.png);
border-color: #ed541d;
}