Different templates depending on URL aliases in drupal7?
visiting url:mysite.com/login,loading template not page.tpl.php,but is login.tpl.php.
How to implement above function?
some code $variables['template_files'][]...in drupal 6 can solve this problem,Why this method won't work in drupal 7?
sorry for my english.

Comments

NIKS_Artreaktor’s picture

Here i found solution. Work for me. (Drupal 7)
forrst.com/posts/Add_page_template_suggestions_depending_on_conte-sDR


function THEMENAME_preprocess_page(&$variables, $hook) {
   // Page template suggestions based off of content types
   if (isset($variables['node'])) { 
                $variables['theme_hook_suggestions'][] = 'page__type__'. $variables['node']->type;
                $variables['theme_hook_suggestions'][] = "page__node__" . $variables['node']->nid;
   }
   
   // Page template suggestions based off URL alias
   if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $template_filename = 'page';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '__' . $path_part;
        $variables['theme_hook_suggestions'][] = $template_filename;
      }
    }
  }
   
}
Alex_JS_OGILVY’s picture

If I've got a node in an alternative language: mysite.com/en/[alias], how must I name it? page--en--[alias] doesn't work.

JohnWoltman’s picture

I wanted to do something similar, but for a particular node. I had to make three changes to make it work for node theme suggestions:

  1. Change the function name to THEMENAME_preprocess_node
  2. Change initial value of template_filename to 'node'
  3. Account for dashes in aliases by adding this line below the second if statement: $alias = str_replace('-', '_', $alias);

So here's what it looks like now:

function THEMENAME_preprocess_node(&$variables, $hook) {
  // Node template suggestions based off URL alias
  if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $alias = str_replace('-', '_', $alias);
      $template_filename = 'node';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '__' . $path_part;
        $variables['theme_hook_suggestions'][] = $template_filename;
      }
    }
  }
}