Drupal页面模板基于url别名

时间:2012-08-02 20:22:32

标签: drupal drupal-7 drupal-theming

我想创建一个drupal页面模板,具体取决于url别名。 她现在的情况:

我创建了一个名为test的网页,其网址别名为test

基于此文档的页面模板 - http://drupal.org/node/1089656为:page--test.tpl.php

我清理了drupal缓存,但仍然显示了此页面的默认页面模板。

可能是什么错误?

1 个答案:

答案 0 :(得分: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