I have a few template node overrides in my theme using Drupal 7.

For example: page--node--53.tpl.php

This might be a lazy request, but is there anyway to name these files instead with the alias I have chosen rather then using the node number? That way I don't have to memorize all of the associated pages.

For example page--node--about.tpl.php


Something like this in template.php should do the trick:

function MYMODULE_preprocess_page(&$vars) {
  if ($node = menu_get_object()) {
    $alias = drupal_get_path_alias("node/$node->nid");

    // Remove slashes from the potential filename
    $clean_alias = str_replace('/', '__', $alias);

    $vars['theme_hook_suggestions'][] = 'page__node__' . $clean_alias;

You might need to massage your $clean_alias variable a bit more to handle any punctuation you're allowing into aliases (e.g. with Pathauto).

A node with an alias like about/about-us would then use a template file called page--node--about--about-us.tpl.php.

  • this worked great for me as well. just made one change because hyphen weren't working for me in the name (e.g. about-us): $clean_alias = str_replace('-', '', str_replace('/', '_', $alias));
    – manishie
    Jul 13 '15 at 18:39

In THEMENAME_preprocess_page (in template.php located in your themes directory) you can set as many template hook suggestions as you wish. Drupal then goes through the list from bottom to top and once it finds the existing template suggestion, it stops and uses that one.

function THEMENAME_preprocess_page(&$vars) {
  // Add theme hook suggestions based on path for nodes only.
  if (isset($vars['node'])) {
    // get whatever you want here into $path variable
    $path = $vars['node']['type'];
    $vars['theme_hook_suggestions'][] = 'page__node__' . $path;

in the $vars there should be the node object that you can check for $vars['node']. also don't forget to sanitize anything you are adding there. good choice would be to use the drupal_clean_css_identifier function


You should look at this thread in which someone has implemented this kind of template suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.