0

I need to add a custom class to my html <body> tag when certain elements are present in content (namely, either a featured image or image gallery, both of which are added to content using Entity Reference fields in the site's main content types).

Currently I'm accomplishing this using the Context module and a taxonomy vocabulary. (Condition: Taxonomy; Reaction: Theme HTML). The workflow for content managers looks like this:

  1. Add an image or gallery to a content item
  2. Don't forget to attach the appropriate taxonomy term so the page lays out correctly.

Oddly enough, forgetting step 2 is a rampant problem.

Any suggestions on methods for eliminating step 2? It seems like it ought to be possible to just test for the presence of a value in the appropriate fields and either automatically attach the taxonomy term, or skip the taxonomy term and trigger the theme HTML based on the data values themselves.

I should mention that the taxonomy vocabulary in question has NO purpose in my application other than applying the appropriate body class.

I'm open to using some other module to accomplish this. (Rules? Couldn't figure out how to make this work either).

(Drupal 7).

2

You could do this in your theme's template.php. Something like this:

function mytheme_preprocess_html(&$vars) {

  // Load node
  $node = menu_get_object();
  if ($node) {

    // Wrap to fetch field values easily 
    $wrapper = entity_metadata_wrapper('node', $node);

    // Check image reference fields and add body class
    if (
      $wrapper->field_featured_image->raw() ||
      $wrapper->field_image_gallery->raw()
    ) {
      $vars['classes_array'][] = 'my-class';
    }
  }
}
| improve this answer | |
  • That works. Unfortunately, I realize there's one special case: the front page doesn't have either of these fields set but still needs the additional class. Actually, it needs it because it is showing a particular block; the ideal general case would be to test for the presence of that block and change the class if it is present. Any thoughts on that? – davidcl Mar 3 '13 at 19:39
  • I should mention that the front page is a node. Is this special case sufficiently different that I should make it a new question? – davidcl Mar 3 '13 at 20:01
  • Got it. For now, I just decided to keep my taxonomy vocabulary and use it to designate special cases. ie, if ($wrapper->field_page_class->raw()) { $variables['classes_array'][] = $wrapper->field_page_class->name->raw(); } – davidcl Mar 3 '13 at 20:27
0

You don't say whether you have any PHP coding ability or not. If you do, the simplest answer might be to add a hook_preprocess_html() implementation to your theme.

| improve this answer | |
  • I have some PHP ability, but I'm not terribly familiar with Drupal internals. How do I test for the presence of data in a content field from hook_preprocess_html()? Pointer to web resource or relevant example would be appreciated. – davidcl Mar 3 '13 at 19:17
  • Never mind, promising code example from Juhog came in while I was typing this comment. – davidcl Mar 3 '13 at 19:17
  • Yes, was about to say something similar. :) – Alfred Armstrong Mar 3 '13 at 19:18

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.