0

I'm trying to implement theme_hook_suggestions in preprocess node template based on the fact that a field (field_logo) is empty or not.

The template change is not working.

Can anyone help?

This is the code that I have so far:

function community_alpha_preprocess_node(&$vars) {
  $node = $vars['node'];
  if(($node->type == 'advertorial') && !$teaser && $node_top){
    $advertorial_type = $node->field_logo['und'][0]['tid'];
    if(!empty($advertorial_type)){
      $vars['theme_hook_suggestions'] = 'node__advertorial';
    }else{
      $vars['theme_hook_suggestions'] = 'node__advertorial__alt';
    }
  }

}

Thanks.

2 Answers 2

0

You need to clear the cache after making changes in the template.php file.

For Drupal 7 version please run below command to clear the cache and check aftewards.

drush cc all

For Drupal 8 version please run below command to clear the cache and check afterwards.

drush cache-clear

11
  • Off course, that's the first thing i did.
    – sly-gdk
    Commented May 31, 2016 at 10:36
  • try with $vars['theme_hook_suggestions'][] = 'node__advertorial';
    – parth.k
    Commented May 31, 2016 at 11:34
  • Thanks parth,.k, i also tried that as well. Other than that, the code makes sense? I wasn't sure about how i used "empty"in the conditional loop.
    – sly-gdk
    Commented May 31, 2016 at 12:26
  • R u sure ? u have tried below code $vars['theme_hook_suggestions'][] = 'node__advertorial'; and clear the cache ?
    – parth.k
    Commented May 31, 2016 at 12:32
  • 1
    checked if its going to if else part or not ?
    – parth.k
    Commented May 31, 2016 at 12:34
0

I believe there was a problem with the first conditional loop. I also removed ['und'], [0] and ['tid'] from field_logo.

So here's the final code that seems to work.

Thanks for the help.

function community_alpha_preprocess_node(&$vars) {
  $node = $vars['node'];
  if($node->type == 'advertorial'){
    $advertorial_type = $node->field_logo;
    if(!empty($advertorial_type)){
      $vars['theme_hook_suggestions'][] = 'node__advertorial';
    }else{
      $vars['theme_hook_suggestions'][] = 'node__advertorial__alt';
    }
  }
}

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.