bratsun
10/29/2014 - 6:34 PM

List field (list_text) formatter: use key as class for value wrapper div

List field (list_text) formatter: use key as class for value wrapper div

$output = '';

// get field info
$field = field_info_field($variables['#field']['field_name']);

foreach ($variables['#items'] as $item) {
  // get key
  $key = $item['value'];
  
  // get label by key
  $label = $field['settings']['allowed_values'][$key];
  
  // use key as class
  $output .= '<div class="field-item ' . $item['value'] . '">' . $label . '</div>';
  
  // OR add an image from predefined /files directory
  // $output .= '<div class="' . $item['value'] . '"><div>'  . theme('image_style', array('path' => file_build_uri('services/' . $item['value'] . '.png'), 'style_name' => '64')) . '</div>' . $label . '</div>';
}
return $output;