首页 > 其他 > 详细

Drupal的$messages是怎么显示的?

时间:2014-05-26 16:36:37      阅读:391      评论:0      收藏:0      [点我收藏+]

Drupal的默认主题bartik会在页面顶部显示系统信息,例如警告、状态等。这个过程是如何实现的?

首先,在bartik目录下找到page.tpl.php,这是bartik主题的页面显示模板。其中有一段是处理$messages的:

bubuko.com,布布扣
<?php if ($messages): ?>
  <div id="messages"><div class="section clearfix">
    <?php print $messages; ?>
  </div></div> <!-- /.section, /#messages -->
<?php endif; ?>
bubuko.com,布布扣


这里的$messages变量从哪里来的呢?在includes/theme.inc文件中找到system_process_page()函数,这是对page hook的处理函数,调用了theme(‘status_messages‘)输出系统信息HTML。

bubuko.com,布布扣
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‘) : ‘‘;
  }
}
bubuko.com,布布扣


status_messages是另外一个theme hook,在system模块的hook_theme钩子中定义:

bubuko.com,布布扣
function system_theme() {
  return array_merge(drupal_common_theme(), array(... ...});
}

function drupal_common_theme() {
  return array(
    ‘status_messages‘ => array(
      ‘variables‘ => array(‘display‘ => NULL),
    ),
  };
}
bubuko.com,布布扣

status_messages hook是用theme_status_messages()函数输出的,在theme.inc里面:

bubuko.com,布布扣
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;
}
bubuko.com,布布扣


theme_status_messages()函数调用drupal_get_message()获取系统信息,与此对应地设置系统信息的函数是drupal_set_message()。

bubuko.com,布布扣
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();
}
bubuko.com,布布扣


从theme_status_messages()可知,系统信息有三类:status、error、warning。每一类系统信息对应一个css样式,在system.messages.css中定义。换句话说,不同类别的系统信息,显示的图标和字体都在system.messages.css中控制。

bubuko.com,布布扣
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;
}
bubuko.com,布布扣

Drupal的$messages是怎么显示的?,布布扣,bubuko.com

Drupal的$messages是怎么显示的?

原文:http://www.cnblogs.com/eastson/p/3746098.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!