Add class from field value to view mode

Handy for themers to have a field value as class in the section wrapper div classes. For example, if you want admins to easily change the color of a block/node using a dropdown interface.

// support for nodes (also in blocks)
function phptemplate_preprocess_node(&$variables) {
  if (isset($variables['field_block_color'][LANGUAGE_NONE][0]['value'])) {
    $variables['classes_array'][] = check_plain($variables['field_block_color'][LANGUAGE_NONE][0]['value']);
  }
}
// support for beans
function phptemplate_preprocess_block(&$variables) {
  if ($variables['block']->module == 'bean') {
    if (isset($variables['elements']['content']['bean'][$variables['block']->delta]['#entity']->field_block_color[LANGUAGE_NONE][0]['value'])) {
      $variables['classes_array'][] = check_plain($variables['elements']['content']['bean'][$variables['block']->delta]['#entity']->field_block_color[LANGUAGE_NONE][0]['value']);
    }
  }
}

About

This is the company blog of
Drupal specialist Merge.nl

We are located in Breda (Netherlands) and build websites using Drupal. More about us.

Content on this blog is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.

Creative Commons License

Recent Comments

Social