I am trying to dynamically display boolean fields into columns based on their value. So for instance these 10 boolean fields on my node, if marked 1, I want it to theme it so that they show up in a column to the left titled Completed, and if marked 0, a column to the right titled Not Complete. I have tried using template.php and field.tpl.php but I am getting very lost.

I'd like to do this on the field node being viewed (not using Views) as a custom dynamic field formatter that applies a different css class to the field div. That or in the theme layer using some php magic.

  • I had a little trouble understanding your question, could edit it to clarify what you're asking? Are you displaying a view? Or is this just a node being displayed?
    – cdmo
    Jan 16 '13 at 1:23
  • This is the display of fields on a node. I'm trying to do this in the theme layer but I don't know where to start. Basically I'm trying to do something similar to drupal.org/project/field_formatter_css_class but rather than apply a css class to the whole node and hide the field, I want to apply it to the field formatter so that I can theme boolean field outputs differently based on their value. Jan 18 '13 at 2:48

Okay I think this might be what you're looking for: http://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_display_alter/7

I'd throw that function on your template.php, enable the Devel module, then use kpr() to print the variables to the screen and go from there.


The answer is easier than I thought. Here is some example code. Just change template in template_preprocess_field to the name of your theme and put it in template.php with an opening php tag.

 * Implements template_preprocess_field()
function template_preprocess_field(&$vars) {
  //check to see if the field is a boolean
  if ($vars['element']['#field_type'] == 'list_boolean') {
    //check to see if the value is TRUE
    if ($vars['element']['#items'][0]['value'] == '1') {
      //add the class .is-true
      $vars['classes_array'][] = 'is-true';
    } else {
      //add the class .is-false
      $vars['classes_array'][] = 'is-false';

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.