好吧,关于breadcrumb.tpl.php在当前主题下面的覆写,我们先介绍到这里。我们已经找到了一个解决办法。现在我们来看另外一个问题,我们知道page.tpl.php它是有模板建议的,而我们的breadcrumb.tpl.php现在还没有这个功能,现在就为它添加模板建议。
我们可以直接到template_preprocess_page里面复制相关的代码即可:
function template_preprocess_page(&$variables) {
….
// Populate the page template suggestions.
if ($suggestions = theme_get_suggestions(arg(), 'page')) {
$variables['theme_hook_suggestions'] = $suggestions;
}
}
这是我依葫芦画瓢,复制过来的样子:
/**
* Implements MODULE_preprocess_HOOK().
*/
function template_preprocess_breadcrumb(&$variables) {
……
// Populate the breadcrumb template suggestions.
if ($suggestions = theme_get_suggestions(arg(), 'breadcrumb')) {
$variables['theme_hook_suggestions'] = $suggestions;
}
}
我开始的时候,复制粘贴修改的时候,做成了这个样子; ' breadcrumb',结果出不来结果,后来调试,发现多了一个空格。复制粘贴的时候,经常会遇到这样的小问题。
如果,要想为模板设置模板建议的话,我们需要设置$variables['theme_hook_suggestions']这个变量。我们这里调用的是theme_get_suggestions,它能够帮助我们返回一个有关模板建议的数组。在template_preprocess_html里面,也是用的这个函数。这个函数的完整结构;
theme_get_suggestions($args, $base, $delimiter = '__')
它包含三个参数,$args,$base,$delimiter,我们这里只用到了前两个。我们看一下,三个预处理函数里面的用法:
theme_get_suggestions(arg(), 'breadcrumb')
theme_get_suggestions(arg(), 'page')
theme_get_suggestions(arg(), 'html')
这个时候,我们就能很好的理解$base的含义了,就是基模板文件的名字。arg()返回的是url里面的参数的数组。
现在,我们在bartik\templates下面,创建一个breadcrumb--user.tpl.php的模板文件,里面加点调试信息,来证明是这个模板文件起作用。清除缓存,访问“user”页面,此时调用的就是我们的模板建议breadcrumb--user.tpl.php了。
我们这里的模板建议规则,和page.tpl.php的保持一致,这是因为,breadcrumb.tpl.php也是基于路径的,这和页面模板文件一样。在block.tpl.php的预处理函数中,模板建议是这样定义的:
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->region;
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module;
$variables['theme_hook_suggestions'][] = 'block__' . $variables['block']->module . '__' . strtr($variables['block']->delta, '-', '_');
它是逐个定义模板建议规则的。