0

I am using a specific content type as blocks (via Nodeblock Module). The nodes have certain taxonomy terms referenced via a term reference field, which I would like to print out in the corresponding block.tpl.php to use them as block classes. Same procedure as one would print terms as body classes, but only in block classes array. I find solutions for printing the term field in node.tpl and page.tpl but not in block.tpl.php Glad for any help on this thanks Matthias

  • Using terms as block classes, as in CSS classes? Look at HOOK_preprocess_block or another Nodeblock-specific hook if necessary. – Ben May 6 '15 at 0:52
  • Thanks Ben. I know this would be the hook to go for, but unfortunately I am no coder – Matthias May 6 '15 at 8:30
1

Here is code tested to work on a minimal install with nodeblock. It's well commented but to recap: get the node displayed in the block, get the term, use one the term's field as css class.

// change mytheme to your theme's name
function mytheme_preprocess_block(&$vars) {
  // don't do anything if it's not a nodeblock block
  if (empty($vars["elements"]["#node"]) || !is_object($vars["elements"]["#node"])) {
    return;
  }

  // get the node object, already loaded
  $node = $vars["elements"]["#node"];

  // get the linked term's id (change field_term to yours)
  $tid = $node->field_term[LANGUAGE_NONE][0]['tid'];

  // load the term fully to get access to its fields
  $term = taxonomy_term_load($tid);

  // turn one of the term's field into a css class (change name to any field)
  $css_class = drupal_clean_css_identifier($term->name);

  // add the css class to the list of classes for this block
  $vars["classes_array"][] = $css_class;
}

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.