Skip to main content

The Cheeky Monkey Media Blog

A few words from the apes, monkeys, and various primates that make up the Cheeky Monkey Super Squad.

Creating Drupal 7 Template Suggestions Based on Path Alias

Drupal 7 has a great system for doing amazing things at the theme level, out of the box it lets you create templates to a specific page. The only draw back is it does this using Drupal internal paths and not the path alias. On the project we are working on we needed all pages which started with resources/ in their path alias to use a specific page template file. After a little hunting found one developed for Drupal 6 and just had to change only a few things to make it work in Drupal 7. Place this snippet in your template.php in the themename_preprocess_page(&$variables, $hook) function and you now have an extra tool at your availability. Drupal 7 Path Alias Page Template Theme Suggestion
Drupal 7 Path Alias Page Template Theme Suggestion
  1. // Get the alias for the page being viewed
  2. $alias = drupal_get_path_alias($_GET['q']);
  3. if ($alias != $_GET['q']) {
  4. 	$template_filename = 'page';
  6.        //Break it down for each piece of the alias path
  7.         foreach (explode('/', $alias) as $path_part) {
  8. 		 // Added the str_replace becayse path parts with - were not being picked up properly by the theme_hook_suggestions
  9.                 $template_filename = $template_filename . '__' . str_replace("-", "_", $path_part);
  10. 		$variables['theme_hook_suggestions'][] = $template_filename;
  11. 	}
  12. }
Updated Feb 17, 2014: handler for template files with - in the name to be handled properly

Web Development

Would you like to know more about what we do?

View Our Services

Graphic Design Cheeky Monkey

Have a look at some our client work

View Our Work

Cheeky Monkey Discovery

Learn more about Cheeky Monkey Media

About Us