1

I have a CT on which I have a field called monetary-type which is of type List (text) and its "Allowed values list" is:

EUR|Euros
USD|Dollars
GBP|Pounds

When this field is rendered from a view (FORMAT: Show: Fields) it's being rendered as:

<span class="views-field views-field-field-monetary-type">
  <span class="field-content">Dollars</span>
</span>

When rendered from a node view it shows as:

<div class="field field-name-field-monetary-type field-type-list-text field-label-above field-wrapper">
  Dollars
</div>

For both those 2 cases I'd like to also have a class with the selected value, in this case value-USD, appended into the classnames of the wrapper.

What is the easiest (non intrusive) way I can achieve this? Ideally without having to touch existing views or node displays.

I use https://www.drupal.org/project/zurb_foundation if it matters.

1

For view field, please implements template_preprocess_views_view_fields()

Reference: https://www.drupal.org/node/1407442#comment-6509858

Code Example

// assume the field name is 'field_text' and
// view name is 'text and view display id is 'page'
function mymodule_preprocess_views_view_fields(&$variables) {
  $view = $variables['view'];
  if ($view->name == 'test' && $view->current_display == 'page') {
    foreach ($variables['fields'] as $id => $object) {
      if ($id == 'field_text') {
        $field_output = $view->style_plugin->get_field($view->row_index, $id);

        if (!empty($field_output)) {
          if ($object->element_type) {
            $class = '';
            if ($object->handler->options['element_default_classes']) {
              $class = 'field-content';
            }

            if ($classes = $object->handler->element_classes($view->row_index)) {
              if ($class) {
                $class .= ' ';
              }
              $class .=  $classes;
            }

            $class .=  ' ' . drupal_html_class("value-$field_output");

            $pre = '<' . $object->element_type;
            if ($class) {
              $pre .= ' class="' . $class . '"';
            }
            $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
          }
        }
        $variables['fields'][$id]->content = $field_output;
      }
    }
  }
}

For node field, please implements template_preprocess_field()

Code Example

// Assume the field name is 'field_text'
function mytheme_preprocess_field(&$variables) {
  $element = &$variables['element'];
  if ($element['#field_name'] === 'field_text') {
    $value = $element['#items'][0]['value'];
    $variables['classes_array'][] = drupal_html_class("value-$value");
  }
}

Note 1: I would recommend using lower case for class name.
Note 2: view_field_name is different from node_field_name

| improve this answer | |
  • Indeed, I didn't test the code. Let me try first. Get you back soon. – Jimmy Ko Aug 10 '16 at 14:28
  • @cherouvim I've fixed above code. Both snippet should work now. Implementing template_preprocess_views_view_fields() seems quite dirty. I am thinking if there is any better way. – Jimmy Ko Aug 10 '16 at 15:30
  • @cherouvim I can't think of other simpler way... LOL – Jimmy Ko Aug 10 '16 at 15:59
0

I used the following:

function mytheme_preprocess_views_view_field(&$vars) {
  if ($vars['field']->field == 'field_monetary_type') {
    $value = $vars['row']->field_field_monetary_type[0]['raw']['value'];
    $vars['output'] = '<span class="value-' . $value . '">' . check_plain($vars['output']) . '</span>';
  }
}

function mytheme_preprocess_field(&$variables, $hook) {
   if ($variables['element']['#field_name'] == 'field_monetary_type') {
     $value = $variables['element']['#items'][0]['value'];
     $variables['classes_array'][] = "value-$value";
  }
}

Note that for the view field I'm wrapping the output into a span containing the class I need.

| improve this answer | |

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.