我们其实在前面已经讲过了,如何新增一个变量。不过我们在Drupal核心的模板文件里面,经常看到这样的代码:
<div class="<?php print $classes; ?>">
而在我们现在的模板文件中,则是这样写的:
<div class="breadcrumb-wrapper contextual-links-region">
这里的class是写死在里面的,我们也想把它改造为变量的形式,好的,我们修改预处理函数里面的代码,这是修改后的样子:
function template_preprocess_breadcrumb(&$variables) {
$variables['classes_array'][] = 'breadcrumb-wrapper';
// Populate the breadcrumb classes.
if ($suggestions = theme_get_suggestions(arg(), 'breadcrumb', '-')) {
foreach ($suggestions as $suggestion) {
if ($suggestion != 'breadcrumb-front') {
// Add current suggestion to breadcrumb classes to make it possible to theme
// the breadcrumb depending on the current page type (e.g. node, admin, user,
// etc.) as well as more specific data like node-12 or node-edit.
$variables['classes_array'][] = drupal_html_class($suggestion);
}
}
}
$current_path = current_path();
$breadcrumb2 = breadcrumb2_load_by_path($current_path);
if(!empty($breadcrumb2)){
$breadcrumbs = array();
// Only keep the first one.
if (!empty($variables['breadcrumb'][0])) {
$breadcrumbs[] = $variables['breadcrumb'][0];
}
$wrapper = entity_metadata_wrapper('breadcrumb2', $breadcrumb2);
$breadcrumb_links = $wrapper->link->value();
foreach($breadcrumb_links as $breadcrumb_link){
$breadcrumbs[]= l($breadcrumb_link['title'], $breadcrumb_link['url']);
}
$variables['breadcrumb'] = $breadcrumbs;
}
if (user_access('administer breadcrumbs') && user_access('access contextual links')) {
$contextual_links_element = array(
'#contextual_links' => array(
'breadcrumb2' => array('breadcrumb', array('0')),
)
);
$contextual_links = array(
'#type' => 'contextual_links',
'#contextual_links' => $contextual_links_element['#contextual_links'],
'#element' => $contextual_links_element,
);
$variables['contextual_links'] = drupal_render($contextual_links);
$variables['classes_array'][] = 'contextual-links-region';
}
// Populate the breadcrumb template suggestions.
if ($suggestions = theme_get_suggestions(arg(), 'breadcrumb')) {
$variables['theme_hook_suggestions'] = $suggestions;
}
// Flatten out classes.
$variables['classes'] = implode(' ', $variables['classes_array']);
}
开始的时候,我没有添加$variables['classes']相关的代码,我是这样想的,系统会调用template_process函数,而在这个函数中,会增加附加的变量classes,不过我想错了。默认处理函数的代码很简单:
function template_process(&$variables, $hook) {
// Flatten out classes.
$variables['classes'] = implode(' ', $variables['classes_array']);
// Flatten out attributes, title_attributes, and content_attributes.
// Because this function can be called very often, and often with empty
// attributes, optimize performance by only calling drupal_attributes() if
// necessary.
$variables['attributes'] = $variables['attributes_array'] ? drupal_attributes($variables['attributes_array']) : '';
$variables['title_attributes'] = $variables['title_attributes_array'] ? drupal_attributes($variables['title_attributes_array']) : '';
$variables['content_attributes'] = $variables['content_attributes_array'] ? drupal_attributes($variables['content_attributes_array']) : '';
}
这里面为我们提供了变量$variables['classes']。但是实际上对于breadcrumb.tpl.php,却没有调用这个处理函数。所以我将这里的代码直接复制到了预处理函数中了。
注意,这里面$variables['classes_array']是一个数组,$variables['classes']是一个字符串。为了生成classes_array这个数组,我们还使用了drupal_html_class这个函数,这些都是从Drupal核心模板的预处理函数中复制过来的。
这是现在的模板文件:
<?php if (!empty($breadcrumb)): ?>
<div class="<?php print $classes; ?>">
<h2 class="element-invisible"> <?php print t('You are here'); ?> </h2>
<?php if (!empty($contextual_links)): ?>
<div style='height:10px'> <?php print $contextual_links; ?> </div>
<?php endif; ?>
<?php //print drupal_render($contextual_link); ?>
<div class="breadcrumb"> <?php print implode(' » ', $breadcrumb); ?> </div>
</div>
<?php endif; ?>