Page + Path * Template = Pleasure

21 Jul in drupal, node, page, snippet
This little snippet saved my bacon today. I decided it was worth a blog post. Sometimes finding the right code is a matter of googling the right words. So I will describe this is making a page template based on the node type you want to present, but you don't you know how to do it on a node, and now you want it on a page. :) Hopefully that will help some SEO.


Here's my Drupal 5 snip

function _phptemplate_variables($hook, $vars) {

    switch($hook) {
        case 'page' :
 
// Change the page.tpl file based on the ailased path
if (module_exists('path')) {
        $alias = drupal_get_path_alias($_GET['q']);
        if ($alias != $_GET['q']) {
          $custom_layouts = array();
          $layout_filename = 'page';
          foreach (explode('/', $alias) as $path_name) {
            $layout_filename = $layout_filename . '-' . $path_name;
            $custom_layouts[] = $layout_filename;
          }
        }
        $vars['template_files'] = $custom_layouts;
      }
      break;
}
    return $vars;
}


Here's one for Drupal 6

function phptemplate_preprocess_page(&$vars) {
  if (module_exists('path')) {
    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));
    if ($alias != $_GET['q']) {
      $suggestions = array();
      $template_filename = 'page';
      foreach (explode('/', $alias) as $path_part) {
        $template_filename = $template_filename . '-' . $path_part;
        $suggestions[] = $template_filename;
      }
      $vars['template_files'] = array_merge((array) $suggestions, $vars['template_files']);
    }
  }
}


Here's some awesome links

Different page templates depending on URL aliases: http://drupal.org/node/139766
modify the preprocess function so that you can prepare and alter page template variables: http://www.informit.com/articles/article.aspx?p=1336146&seqNum=7 Hopefully this is as helpful for you...

Comments

Post new comment