Working with template suggestions depending on the current path in Drupal

November 11, 2013
1K
12K


Category:
Tags:

Do you have the following questions in mind about Template suggestions based on DRUPAL path ?

  • Using different page templates depending on the current path
  • Template suggestions based on Path alias
  • Change templates based on path alias
  • How to Enable templates based in Path URL with Drupal6 / Drupal7
  • Drupal Page Template based on url alias
  • How do I get the path of the current drupal theme
  • How to get the path to your theme in your drupal template
  • Change Drupal Theme Based On URL Path

Steps to solve

  • You must install path module
  • Write a yourthemename_preprocess_page() function in template.php file inside currently active theme folder.
  • Create a template file based on path & Clear your site cache

Code for Drupal 6

<?php
/**
 * implements theme_preprocess_page() function
 */
// drupal6 code
function yourthemename_preprocess_page(&$vars) {
  
// create template based on url aliases if path module is enabled
  
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;
        
$vars['template_files'][] = $template_filename// drupal6
      
}
    }
  } 
}
?> 

Code for Drupal 7

<?php
/**
 * implements theme_preprocess_page() function
 */
// drupal7 code
function yourthemename_preprocess_page(&$vars) {
  
// create template based on url aliases if path module is enabled
  
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) {
          
// d7 contains two underscore _ after page. 
         // During creating tpl file underscore (_) is converted to dash (-)   i.e page--gallery.tpl.php
        
$template_filename $template_filename '__' $path_part;         $vars['theme_hook_suggestions'][] = $template_filename// drupal 7
      
}
    }
  } 
}
?> 

Example

Create preprocess function based on your theme

Suppose your current path is: http://localhost/d7multilingual/company-profile and if your active theme name is webemania then function name will be webemania_preprocess_page() your active drupal themes directory is: sites/all/themes/webemania. Create a template.php file inside webemania directory if not already exists and copy & paste above code So your final code look like (for drupal 6)

<?php
/**
 * implements theme_preprocess_page() function
 */
// drupal6 code
function webemania_preprocess_page(&$vars) {
  
// create template based on url aliases if path module is enabled
  
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;
        
$vars['template_files'][] = $template_filename// drupal6
      
}
    }
  } 
}
?> 

 

Create template file & clear all cache

  • For Drupal6, create a template file page-company-profile.tpl.php inside your theme directory
  • For Drupal7, create a template file page--company-profile.tpl.php inside your themes template directory

Add some custom code inside tpl file like "This is company profile tpl file" as if you will examine later after visiting url. Now flush all cache of your site and browse http://localhost/d7multilingual/company-profile. You will see your desired content