user998163
user998163

Reputation: 483

Drupal Page Template based on url alias

I want to create an drupal page Template depending on the url alias. Her my current situation:

I create a page named test, the url alias is test, too.

The page template, based on this docu - http://drupal.org/node/1089656 is: page--test.tpl.php.

I cleaned the drupal them cache, but there is still the default page template shown for this page.

What could be the error?

Upvotes: 1

Views: 2839

Answers (1)

nmc
nmc

Reputation: 8686

page--test.tpl.php doesn't work because Drupal is using the real path of page--node--#.tpl.php. To get Drupal to recognize aliased paths, you have to add the aliased path as part of the theme suggestions like so:

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;
      }
    }
  }
}

Source: http://groups.drupal.org/node/130944#comment-425189

Upvotes: 1

Related Questions