0

I have a site which has two main views, a men's blog and a women's blog, both with their own content types, mens-blog-post and womens-blog-post. The problem I am facing is that they share the same page--node.tpl file (node page view, default file:page.tpl not node.tpl), but need to have different content (e.g. other relevant views in the sidebar region). I need to suggest two new template files called page--node--mens-blog-post.tpl.php and page--node—womens-blog-post.tpl.php based on content type.

Here is my attempt:

function force_preprocess_page(&$vars){
 if ($vars['content']->type == 'mens-blog-post') {
  array_unshift($vars['theme_hook_suggestions'], 'page__node__mens_blog_post');
 } 
 if ($vars['content']->type == 'womens-blog-post'){
  array_unshift($vars['theme_hook_suggestions'], 'page__node__womens_blog_post');
 } 
}

Can anyone help me clean this up and make it work? I am looking for any solution that works so I am open to other ideas.

  • Which errors do you get, or in which way isn't the code you wrote working? (You wrote, "help me clean this up and make it work.") – kiamlaluno Jan 3 '12 at 23:18
  • I'm not getting any errors the code just doesn't suggest the tpl file I need. I just assumed someone who knows this stuff well could take one look at it and see why it doesn't work. – KenAesthetic Jan 4 '12 at 18:14
7

To use a different template file for the node being shown, you need to add a new suggestion in $variables['theme_hook_suggestions'] (Drupal 7), or $variables['template_files'] (Drupal 6). In both the cases, the variable is an array.

In a module, the hook to implement is hook_preprocess_page().

function force_preprocess_page(&$variables) {
  if (!empty($variables['node'])) {
    if ($variables['node']->type == 'mens-blog-post') {
      array_unshift($variables['theme_hook_suggestions'], 'page__node__mens_blog_post');
    }
    elseif ($variables['node']->type == 'womens-blog-post') {
      array_unshift($variables['theme_hook_suggestions'], 'page__node__mens_blog_post');
    }
  }
}

If you have a module called force.module, then your code is correct. The same code can be used for a theme whose short name is force; in this case, in its template.php file you add a force_preprocess_page() function.
In Drupal 6, your theme needs to also have the default template file (page.tpl.php), or the custom template is not going to be picked up. I didn't check if this is still valid in Drupal 7. (The only time I created a theme that was using a custom template file was in Drupal 6.)

I would rather check the variable $variables['node'] is not empty, as hook_preprocess_page() is called for every page served from Drupal, including the ones not associated with any node.

2

I would use Panels instead of a suggestion.

  1. Active the "node view" page
  2. Create two variants, with
    1. One selection rule for "mens-blog-post"
    2. Another selection rule "womens-blog-post"
  3. Add "Node content" to a region.

Done.

Relevant video tutorials here: http://dev.nodeone.se/en/learn-page-manager first four videos should get you started :)

  • I tried what you suggested and it partially worked. My first problem is the panel is still using the same page--node.tpl so nothing has changed really just more complicated, secondly the custom breadcrumb trail no longer works, and thirdly I am getting a ton of errors about memory... any idea what I need to do to clean this up? You made it sound so easy... – KenAesthetic Jan 3 '12 at 5:07
  • Warning: require_once() [function.require-once]: Unable to allocate memory for pool. in _drupal_bootstrap_full() (line 4909 of /Users/Ken/Sites/fource7/includes/common.inc). Warning: include_once() [function.include-once]: Unable to allocate memory for pool. in drupal_load() (line 1114 of /Users/Ken/Sites/fource7/includes/bootstrap.inc). – KenAesthetic Jan 3 '12 at 5:08
  • 1) Custom breadcrumb, is indeed a problem, but has a patch here: drupal.org/node/1251520. 2) If you have verified that the node.tpl.php really is in use, then the selection rules are not configured correctly. 3) I How much memory does PHP have? – Letharion Jan 3 '12 at 7:01
  • Did you actually watch any of the videos? That memory warning looks APC related, and probably has nothing to do with PHP's memory. stackoverflow.com/questions/3723316/… – Letharion Jan 3 '12 at 7:15
  • I did. Maybe I need to go back over them. I found that the breadcrumbs don't work only when I specify to serve the body content. When I specify the node's full content the breadcrumbs do work. Any idea why that is? Can you explain? – KenAesthetic Jan 3 '12 at 19:36
0

I ended up using context (http://drupal.org/project/context) to swap out the content based on the content type. Big thanks to gilzero on my other post... sorry for the mess won't happen again.

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.