2

I have a custom views formatter.

function template_preprocess_views_view_flot_views_time(&$variables) {
  $view = $variables['view'];
  $field_name = 'foo_bar';
  foreach ($view->result as $row) {
    $field_value = $view->field[$field_name]->getValue($row);
  }
}

This works when the field is a single-value field. However, I have a couple multi-value fields. The result of the above expression is always "1" for these fields. These fields can be identified because

 $view->field[$field_name]->multiple == TRUE

I can use

kint($view->field[$field_name]->getItems($row));

to see that this is an array with a render array and raw data. The value as a string can be found at:

$view->field[$field_name]->getItems($row)[0]['rendered'][#markup']

If the multi-value field is named foo, and has three parts, 'bar', 'baz', and 'qux', is there a better way to get the value of foo_bar or foo_baz?

I've been looking at the documentation for core/modules/views/src/Plugin/views/field/Field.php but it's not helping.

1

You don't need to search for another method, Field::getValue() returns the array you are looking for:

if ($view->field[$field_name]->multiple) {
  $field_array = $view->field[$field_name]->getValue($row);
}

I don't know if there is a documentation, but you can see this in code:

https://api.drupal.org/api/drupal/core!modules!views!src!Plugin!views!field!Field.php/function/Field%3A%3AgetValue/8.2.x

Difference between field values from view result and entity

If you have a standard multivalue field like text or number in a view and don't change any options, getValue($row) returns a one-dimensional array with the values from the main property of the field. If not you have somehow configured the view so that it shows a different result.

You can although get the original entity field data, which is independent from the configuration of the view. For this the field needs to be in the row entity:

$field_array = $row->_entity->{$field_name}->getValue();

This is then a two-dimensional array with multiple values and multiple properties.

|improve this answer|||||
  • In my installation, it returns $args array(1) →array(1) string(1) "1", while the '#markup' method in my question returns '99.000', the correct value. – Kevin Nowaczyk Feb 9 '17 at 19:31
  • getItems() collects the data differently, so the result may be different. Not all fields in views are real entity fields. – 4k4 Feb 9 '17 at 19:44
  • What I meant is kint($field_array = $view->field[$field_name]->getValue($row)) returns $args array(1) →array(1) string(1) "1" instead of string(6) "99.000" – Kevin Nowaczyk Feb 9 '17 at 19:47
  • That's what I meant, the result of both method has not to be the same. Try another field or another view. The field handling of views is sometimes a bit strange, dependent on how a view is configured. – 4k4 Feb 9 '17 at 20:09
  • Got a chance to test your code in one of my views. See the edited answer. – 4k4 Feb 10 '17 at 7:48

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.