1

In drupal 7, I created a content type called page and set the url to /about. If I check the source code I see

<link rel="canonical" href="/website/about"> 

and

<link rel="shortlink" href="/website/node/7" />

So if I create a new template called page--node--7.tpl.php then it works. But if I create a new template called page--about.tpl.php it wont get picked up.

What am I doing wrong here?

Thanks!

1

I think you have issue with hook_suggestions that we need to configure correctly for a template to work.

Its already answered here

drupal 7 template suggestions based on path alias

Hope it will help!

  • Worked like a charm, thanks for pointing me to it! – Jean Sep 8 '14 at 12:07
  • @Jean if this is the answer to your question, you used it and it worked, consider marking it as accepted. It is in no way obligatory, but it tells other users with the same problem "this is what I did, and it worked for me" - pretty useful piece of information, right? :) – Mołot Sep 8 '14 at 13:35
1

You aren't doing anything wrong per se Jean.

I am assuming that you created the content type page and then created a node of the type page. To create a template for just the page you just created and not all of the instances of the content type page, using the node ID is a fairly standard solution. If you want to have that template for all instances of the content type page then you would use page--Content_Type_MACHINE_NAME.tpl.php

You also need to add a preprocess function for that to function:

function THEME_preprocess_page(&$vars, $hook) {
  if (isset($vars['node']->type)) {
    // If the content type's machine name is "my_machine_name" the file
    // name will be "page--my-machine-name.tpl.php".
    $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
  }
}

I am curious about naming your new content type page since Drupal has a Basic Page content type with the machine name of page already. What is the machine name of your new content type?

If you absolutely must have the template based on the URL then it looks like you need a different preprocess function. I'd think twice about approaching it this way since it is so easy to change the path/alias. You will need to install the path module for this preprocess function to work:

<?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
      }
    }
  } 
}
?>
  • I do have the path module installed. I set the machine name to "about", I used the code from the comment above and it worked nicely. But from what I gather you suggest to just keep it at page--node--7 etc. – Jean Sep 8 '14 at 12:10
  • I do. Only because the page template will not be applied if the alias is changed. Probably not a big issue with your About page. – Jon Reid Sep 8 '14 at 12:47

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.