1

Not sure that is the best title...

I want add a field to a content type eg. Priority. My preference would be to make this a taxonomy reference (with Autocomplete term widget (tagging)).

Then probably through a preprocess I would like to add any terms entered as classes to the node (in particular when a teaser but that's not important).

I am not the world's greatest php coder and apologise if this doesn't make a great deal of sense. I have been banging my head against the wall for three days now and any help would be greatly appreciated :)

I gather it has something to do with MYTHEME_preprocess_node and $variables['classes_array'][], but I don't seem to be able to put my finger on it.

2 Answers 2

4

This should be a start.

function mytheme_preprocess_node (&$variables)
{
  $node = $variables["node"];

  if (isset($node->field_foo["und"][0]["taxonomy_term"])) {
    foreach ($node->field_foo["und"] as $foo) {
      $term = $foo["taxonomy_term"];
      $variables["classes_array"][] = "term-" . str_replace(" ", "-", strtolower($term->name));
    }
  }
}

Note, this is barely tested and your exact situation may vary. It assumes field_foo is the machine name for your taxonomy reference, and your theme is flattening $variables["classes_array"] for use in node.tpl.php.

3
  • At this point classes_array have already been converted, so this wouldn't work on the standard node.tpl.php
    – googletorp
    Feb 8, 2012 at 12:32
  • 1
    @googletorp, I subthemed Bartik and tested the above code, and I do see the classes on the <div> that contains the node. template_process has the default that implodes classes_array into classes, so this will run after all preprocess hooks.
    – mpdonadio
    Feb 8, 2012 at 13:55
  • So, sorry for taking so long to come back. It turns out that if you put the above code in template.php, in the RIGHT directory (I am feeling so stupid right now! Obviously not good at multi tasking..), then it works perfectly! Thanks a million MPD, and googletorp, I owe you guys a coffee! Feb 8, 2012 at 16:36
0

It's not that hard.

Your problem is that you are chaining the wrong variable, this should work:

function MYTHEME_preprocess_node(&$vars) {
  $vars['classes'] .= ' foo bar baz'; // $classes is a text string.
}
3
  • Thanks for your help! But... how would you replace = 'foo bar baz'; with = 'contents of field_priority array';? Feb 7, 2012 at 18:09
  • 2
    I though best practice was that these were kept as arrays until the process stage, which flattens them to strings? Also, this varies a little theme to theme. Zen (and subthemes) prints out $classes in node.tpl.php, so you would append to $variables['classes_array'] in the preprocess. Omega (and subthemes) prints out $attributes in node.tpl.php, so you need to append to $variables['attributes_array']['class'] in the preprocess.
    – mpdonadio
    Feb 7, 2012 at 19:37
  • @MPD I have to defer to you on best practice. I am not creating a sub theme, and most everything I have read refers to $variables['classes_array'] so I am pretty sure that is where it belongs. The main problem I have is how to get the content of the array? Do you think I can get strings at process and then pass them into $variables['classes_array'] or... If you could give an example to get me going that would be awesome! I think my brain has definitely melted :( Feb 7, 2012 at 23:24

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.