0

How to get the views field value and make a condition base on that field. Let say may views field is 'field_state'

Example below:

function my_module_views_pre_render(ViewExecutable $view) {
  //current user id
  $user_id = \Drupal::currentUser()->id();
  //load user data
  $user = \Drupal\user\Entity\User::load($user_id);
   // Get user state
  $state = $user->get('field_state')->getValue()[0]['value'];  
  if ($state == Views field_state value) {
        $view->field['field_state']->options['exclude'] = TRUE; // exclude field for only this result ???
  }
  else{
    $view->field['field_state']->options['exclude'] = FALSE; // include field for only this result ??? 
  }
}

Is it possible on views pre-render or I need to use the different hook?

1 Answer 1

0

You should try hook_views_post_build(). I haven't tried this but I was looking at the views api and noticed that hook_views_post_build shows an example of modifying the exclude property of a field.

Your Answer

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

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