I am trying to alter a views result after it's executed, and before it's rendered. I was originally using hook_views_pre_render(viewExecutable $view)
, but that wasn't updating any of the view results for one reason or another, so I switched over to hook_views_post_execute(viewExecutable $view)
which DOES update view results, however, both hooks only work immediately after a cache clear.
Is there a reason this was done? It seems pointless to have these hooks if they only fire based on cache clears or cache being updated.
I am using the hook to compare a field in a result row to an array, and if it doesn't match a value in the array, then it removes the views result so it doesn't show up in the view.
Like I said in the post, this works in the initial view visit immediately after a cache clear, but as soon as you refresh the page the view is no longer using the hook. I've disabled views caching in settings, but it still seems to be behaving this way.
I am also curious why my normal views display responds to the hooks, but my REST export display does not. Does it need different hooks?
The exact code I am using is below:
function tcrp_server_status_views_post_execute(\Drupal\views\ViewExecutable $view) {
if ($view->id() == "server_list") {
$steamid = \Drupal::request()->query->get('steamid');
if ($steamid == NULL || $steamid == '') {
unset($view->result);
}
$u = \Drupal::entityQuery('user')
->condition('field_steam_id',$steamid);
$result = $u->execute();
$user = \Drupal\user\Entity\user::load($result[1]);
foreach ($view->result as $k => $v) {
$display = false;
foreach ($v->_entity->field_access_roles->referencedEntities() as $reference) {
if (array_search($reference->id(),$user->getRoles())) {
$display = true;
}
}
if (!$display) {
unset($view->result[$k]);
}
}
}
}
I've disabled views caching in settings, but it still seems to be behaving this way.
.$view->element['#cache']['contexts'][] = 'url.query_args:steamid';