0

I've added some image and link content to region-sidebar.tpl.php before it executes the print $content. All good, except now the client wants the images and links specific to content type. (I can't do it all in CSS, otherwise i'd go that route).

How can I test for content type in region-sidebar.tpl.php or have a variable that is set. The region-sidebar.tpl.php doesn't see a variable defined in preprocess_node.

In region-sidebar.tpl.php I need something like:

<?php if ($content): 
  if ($mycontent_type == "foo") {
       ?> < add some html stuff here> <?php
  } 
  print $content; ?>

1 Answer 1

0

You can get the current node like this:

if ($node = menu_get_object()) {
  if ($node->type == 'foo') {
    ...
  }

But don't put that logic directly in a template file - move it out to a preprocess function instead.

3
  • You're saying, in the theme's template file, add a preprocess_region function with the above? By referencing the $node variable, if it isn't created at this point, it gets created? So in the region-sidebar template file, it i could then reference it? I could add custom variables to it, like $variables?
    – Dubya
    Commented Apr 16, 2014 at 11:04
  • Yep exactly - menu_get_object() should always give you the node object if you're on a node page, it'll be available in preprocess hooks
    – Clive
    Commented Apr 16, 2014 at 11:06
  • I'm ok testing in preprocess_region fn() for a sidebar region on a specific node type. All good, but then if i'm in the region-sidebar template file, it doesn't have access to $node. How do i set a varialble that i can test in there?
    – Dubya
    Commented Apr 16, 2014 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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