0

I have a Boolean field on a content type. Possible values are completed and not completed. The default rendering is not satisfactory though since it doesn't include any CSS class to represent the boolean state (true, false) so I cannot act on this using CSS or JavaScript.

<div class="field 
            field-name-field-status
            field-type-list-boolean
            field-label-inline
            clearfix">
  <div class="field-label">Completion status:&nbsp;</div>
  <div class="field-items">
    <div class="field-item even">completed</div>
  </div>
</div>

What's the best way to add such a class on .field or .field-item?

What I've tried so far:

  • hook_field_display_alter but this doesn't provide me with data nor field type
  • hook_node_view but I cannot iterate fields by type. I'd need to pinpoint the specific field instance I need to modify but I don't know that in advance.
  • by providing a custom node template, but this solution is not generic for all occurrences of a boolean field
3

You can implement hook_preprocess_field() and add the class there.

Get field type :

$element = $variables['element'];
$type = $element['#field_type'];

Add something to field class :

$variables['classes_array'][] = 'class-name';

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.