_preprocess_node examples


/ Published in: PHP
Save to your folder(s)

sample code for the drupal preprocess_node function. Some of these are written specifically for a Zen subtheme.

Read code comments for details...


Copy this code and paste it in your HTML
  1. function ZENSUBTHEME_preprocess_node(&$vars, $hook) {
  2. // $vars['sample_variable'] = t('Lorem ipsum.');
  3. $classes = array();
  4.  
  5. // add a class to identify the taxonomy term(s) for the specified node types and taxonomies
  6. // SET type - specify the node types this class is needed for.
  7. if ($vars['type']=='group_recipe') {
  8. $node_terms = $vars['node']->taxonomy;
  9. foreach ($node_terms as $term) {
  10. // check if the term belongs to the taxonomy which requires a node class and if so, set the node class using the term id.
  11. // SET vid - specify the vid of the taxonomy which should be identified in the class
  12. if ($term->vid == 5) {
  13. // using $term->tid instead of $term->name because name can change.
  14. $classes[] = zen_id_safe($vars['type'] . '-type-' . $term->tid);
  15. }
  16. }
  17. }
  18.  
  19. // add a class to identify if this post is from the admin of the group.
  20. // NOTE that this method may have problems when posted to multiple groups because this function does not try to identify the current group being viewed.
  21. foreach ($vars['og_groups'] as $node_group_id) {
  22. if ($vars['teaser']) { // NOTE: it should not be a problem to load the group node since it will already be loaded on pages that list group nodes, but it may be good to isolate this class to only appear on teasers
  23. $group_author = node_load($node_group_id)->uid;
  24. if($group_author == $vars['uid']){
  25. $classes[] = 'by-group-admin';
  26. }
  27. }
  28. }
  29.  
  30. // add these new classes to the classes declared by zen.
  31. $vars['classes'] .= ' ' . implode(' ', $classes); // Concatenate with spaces
  32.  
  33. // dpm($vars);
  34. // dpm($vars['classes']);
  35. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.