0

So a basic question. On Drupal 8 I have a views field override using preprocess. I need to get the value of the field but everything I try returns null or an error. I have:

function mytheme_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];
  $id = $view->storage->id();
  if ($id == "myfield"){
    $myfieldvalue = $variables['field'];
  }
}

I have tried various combinations. I thought getValue() might work but no. So far $variables['field']->original_value is closest but obviously returns the markup version.

The $variables I cannot see the raw value. So question is how do I get it? Do I need to load the node and get it that way?

1

Calling getValue() of a views field object should work, but you need to provide the row as parameter:

$myFieldValue = $variables['field']->getValue($variables['row']);

Reference: https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21field%21FieldPluginBase.php/function/FieldPluginBase%3A%3AgetValue/8.5.x

1

This is the solution I came up with pass row number and field name

$field_output = $view->style_plugin->getFieldValue(0, 'field_custom_text');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy