Return to Snippet

Revision: 27870
at June 25, 2010 11:00 by MikeyLikesIt


Initial Code
function ZENSUBTHEME_preprocess_node(&$vars, $hook) {
//  $vars['sample_variable'] = t('Lorem ipsum.');
  $classes = array();

  // add a class to identify the taxonomy term(s) for the specified node types and taxonomies
  // SET type - specify the node types this class is needed for.
  if ($vars['type']=='group_recipe') {
    $node_terms = $vars['node']->taxonomy;
    foreach ($node_terms as $term) {
      //  check if the term belongs to the taxonomy which requires a node class and if so, set the node class using the term id.
      //  SET vid - specify the vid of the taxonomy which should be identified in the class
      if ($term->vid == 5) {
        // using $term->tid instead of  $term->name because name can change.
        $classes[] = zen_id_safe($vars['type'] . '-type-' . $term->tid);  
      }
    }
  }
  
  // add a class to identify if this post is from the admin of the group.
  // 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.
  foreach ($vars['og_groups'] as $node_group_id) {
    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
      $group_author = node_load($node_group_id)->uid;  
      if($group_author == $vars['uid']){
        $classes[] = 'by-group-admin';
      }
    }
  }

  // add these new classes to the classes declared by zen.
  $vars['classes'] .= ' ' . implode(' ', $classes); // Concatenate with spaces

//  dpm($vars);
//  dpm($vars['classes']);
}

Initial URL


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

Read code comments for details...

Initial Title
_preprocess_node examples

Initial Tags


Initial Language
PHP