【问题标题】:Drupal Page Template based on url alias基于 url 别名的 Drupal 页面模板
【发布时间】:2012-08-02 20:22:31
【问题描述】:
我想根据 url 别名创建一个 drupal 页面模板。
她我现在的情况:
我创建了一个名为test的页面,url别名也是test。
基于此文档的页面模板 - http://drupal.org/node/1089656 是:page--test.tpl.php。
我清理了 drupal 他们的缓存,但仍然显示此页面的默认页面模板。
可能是什么错误?
【问题讨论】:
标签:
drupal
drupal-7
drupal-theming
【解决方案1】:
page--test.tpl.php 不起作用,因为 Drupal 使用的是 page--node--#.tpl.php 的真实路径。要让 Drupal 识别别名路径,您必须将别名路径添加为主题建议的一部分,如下所示:
function MYMODULE_preprocess_page(&$vars, $hook) {
// only do this for page-type nodes and only if Path module exists
if (module_exists('path') && isset($vars['node']) && $vars['node']->type == 'page') {
// look up the alias from the url_alias table
$source = 'node/' .$vars['node']->nid;
$alias = db_query("SELECT alias FROM {url_alias} WHERE source = '$source'")->fetchField();
if ($alias != '') {
// build a suggestion for every possibility
$parts = explode('/', $alias);
$suggestion = '';
foreach ($parts as $part) {
if ($suggestion == '') {
// first suggestion gets prefaced with 'page--'
$suggestion .= "page--$part";
} else {
// subsequent suggestions get appended
$suggestion .= "__$part";
}
// add the suggestion to the array
$vars['theme_hook_suggestions'][] = $suggestion;
}
}
}
}
来源:http://groups.drupal.org/node/130944#comment-425189