0

how can I access the value of another field inside the preprocess_views_view_field. This is an example:

function THEMENAME_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];
  $field = $variables['field'];

  if ($view->id() == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME') {
    if ($field->field == 'FIELD_NAME') {
      $my_output = manipulation(ANOTHER_FIELD_NAME_IN_VIEW);
      $variables['output'] = $my_output;
    }
  }
}

It's possible or should i use it _preprocess_views_view?

4
  • Have a look in $variables['row'] that should contain all the fields in your view
    – Leigh
    Jan 22, 2020 at 10:39
  • Should be you can see it here api.drupal.org/api/drupal/… you can see $variables['row'] being used
    – Leigh
    Jan 22, 2020 at 10:52
  • I am using _preprocess_views_view_field single with no final s. Maybe it's not possible.
    – carlo
    Jan 22, 2020 at 11:04
  • Ok, I solved, thanks your suggestion helped me.
    – carlo
    Jan 22, 2020 at 11:26

1 Answer 1

2

Ok, I solved it like this:

function THEMENAME_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];
  $field = $variables['field'];
  $rows = $variables['rows'];

  if ($view->id() == 'VIEW_NAME' && $view->current_display == 'DISPLAY_NAME') {
    if ($field->field == 'FIELD_NAME') {      
      foreach ($view->field as $fieldId => $field) {
        if ($fieldId === 'ANOTHER_FIELD_NAME_IN_VIEW') {
          $my_output = $view->style_plugin->getFieldValue($row->index, $fieldId);
        }
      }
      $variables['output'] = $my_output;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.