Hi,

I'm using Drupal 7, and have created a subtheme for Bartik. I would like to customize the template for the book module. I was able to create a custom node template using the filename: node--book.tpl.php , but I'm not able to create a custom page template for the book module. I've tried different file names like:

* page-book.tpl.php
* page--book.tpl.php
* page-node-book.tpl.php

but no luck.

Using the theme devel module, on clicking the page elements, I'm surprised to see it's referencing the file html.tpl.php on the book pages, while on other pages the same area it references page.tpl.php.

So I'm confused what to do, how to create a custom page template for a content type (like book) in D7 using a subtheme.

thanks

Comments

Bahlool’s picture

Ok, I was searching around for solutions and realized that templates.php needs to be modified for Drupal to be able to create custom page templates for content types. I came across this code:

http://adaptivethemes.com/page-template-suggestions-per-node-type

and have added to template.php of my Drupal 7 Bartik sub theme. But no luck :(

The code in the above link is for Drupal 6. Is it compatible for Drupal 7?

Jeff Burnz’s picture

I updated that post to include a snippet for Drupal 7, the main different is that we now use theme_hook_suggestions and the syntax is slightly different for templates - use two hyphens instead of one:

function themeName_preprocess_page(&$vars, $hook) {
  if (isset($vars['node'])) {
  // If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
   $vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
  }
}
Bahlool’s picture

Hi Jeff,
That worked! Thanks, you're too cool! B-)

Anonymous’s picture

Thanks, I added 'node__' to avoid conflicts with custom (Views) paths.

function dawson_preprocess_page(&$vars, $hook) {
  if (isset($vars['node'])) {
  // If the node type is "blog" the template suggestion will be "page--node--blog.tpl.php".
   $vars['theme_hook_suggestions'][] = 'page__node__' . str_replace('_', '--', $vars['node']->type);
  }
}
dgastudio’s picture

thank u!!

clcanela’s picture

Plain and simple, thanks! now i can do page--node--product!!! and my views pages paths still work.
Great!

feo’s picture

Thank You very much.

Jennifer_M’s picture

Thanks Jeff, that worked for me too!

For anyone wanting to remove sidebars from their alternative page layout, here's a useful article it took me a while to find:
http://www.lullabot.com/articles/drupal-performance-tip-block-visibility

(Despite the article name, it's not only a "performance tip": it explains some stuff about classes and CSS which you might need in order to make the sidebar disappear properly.)

sahoo100’s picture

thanks jeff its working fine........

Bahlool’s picture

Anyone with any ideas?

Aniara.io’s picture

I'm interested in this too, will run some tests later today. For the moment, try replacing &$vars with &$variables and see it makes any difference.

Jeff Burnz’s picture

$vars is just a short version of $variables, it depends which one you declare as the parameter in your preprocess function, for example the following two examples are both correct:

// use $variables
function themeName_preprocess_page(&$variables, $hook) {
  $variables['whatever'] = 'whatever man';
}

// use $vars
function themeName_preprocess_page(&$vars, $hook) {
  $vars['whatever'] = 'whatever man';
}
Meepu’s picture

Thanks for mentioning the filename for custom node template. That solved my problem. :)

felixvang’s picture

I'm sorry and I'm pretty new to this stuff. Where exactly should I paste on the template.php? I've tried numerous from top to bottom and I got errors. After paste the code is there any extra steps I need to know?

thanks..

giorgio79’s picture

Could this be done from a module so we dont have to update the theme template.php every time there is a new version?