1

I have just introduced my custom template suggestions in MYTHEME_preprocess_node, based on a certain field's value. Won't work somehow.

The content type is project. In my template folder I have already a file named node--project.tpl.php, and my suggestions append additional two: node--project--pcgame.tpl.php and node--project--movie.tpl.php. They are visible when I dpm() the variables, but are not found/applied to the node, even when I remove node--project.tpl.php.

Cache cleared.

Any clue as per what could be the cause?


The code I'm using:

function MYTHEME_preprocess_node($variables){
  if(isset($variables['node'])){
    $node = $variables['node'];
    if($node->type == 'project'){
      // Determine project type based on a term field.
      $project_type = (int) $node->field_genre['und'][0]['tid'];

      switch($project_type){
        case 4:
          $variables['theme_hook_suggestions'][] = 'node__project__pcgame';
          break;
        default:
          $variables['theme_hook_suggestions'][] = 'node__project__movie';
      }
      dpm($variables['theme_hook_suggestions']);
      }
   }
}
  • what version of drupal? – Petro Popelyshko Dec 13 '12 at 14:00
  • Could you show the code that creates the template suggestions? – Clive Dec 13 '12 at 14:00
  • D7. Just provided the code for you. – Artur Dec 13 '12 at 14:08
  • 2
    Is that the exact code? $variables needs to be passed by reference, i.e. function MYTHEME_preprocess_node(&$variables){ – Clive Dec 13 '12 at 14:09
  • 1
    Because the local copy of $variables contains it :) – Clive Dec 13 '12 at 14:13
0

You try this code.

 function youthemename_preprocess_node(&$variables) {
 $node = $variables['node'];
    if($node->type == 'project'){
      // Determine project type based on a term field.
      $project_type = $node->field_genre['und'][0]['tid'];

      switch($project_type){
        case 4:
          $variables['theme_hook_suggestions'][] = 'node__project_pcgame';
          break;
        default:
          $variables['theme_hook_suggestions'][] = 'node__project_movie';
      }
    }   
}

You file name should be node--project_pcgame.tpl.php and node--project_movie.tpl.php. Clear performance cache, it should call hook_theme_registry_alter(&$theme_registry).

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.