1

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]);
      }
    }
  }
}
5
  • 1
    If you really don't want caching on the view you can check and set caching to be none (on view edit page under advanced section). – Ales Aug 24 '20 at 7:02
  • @Ales he/she already did, I've disabled views caching in settings, but it still seems to be behaving this way.. – No Sssweat Aug 24 '20 at 7:49
  • For anonymous requests in general see this issue (not Views specific) drupal.stackexchange.com/questions/207800/… – 4k4 Aug 24 '20 at 8:00
  • 1
    I was not able to reproduce, but try adding a cache context $view->element['#cache']['contexts'][] = 'url.query_args:steamid'; – No Sssweat Aug 24 '20 at 9:36
  • @Ex0r when it comes to REST export display, that's a different beast. Hooks do not work on REST exports. You'll need to create a custom serializer, see How to change view REST export format – No Sssweat Aug 24 '20 at 15:06
0

Your actual issue is this part of the code:

if (array_search($reference->id(),$user->getRoles())) {
   $display = true;
}

Since $reference->id() is a number, this conditional will always fail to pass.

For the conditional to pass, it would have to be:

if (array_search('role_machine_name',$user->getRoles())) {
   $display = true;
}
4
  • While I agree the machine name needs passed, in the case of this, $reference->id() is returning the machine name. When dumping $reference->id() it returns the machine name and the role, not a number. The code above works just fine, but only on the first visit after a cache clear. After that, changes aren't updated in the view. Adding in your $view->element['#cache']['contexts'][] = 'url.query_args:steamid'; did appear to fix the caching issue. NOPE, take that back. If I close the browser, and come back in, the view is reset until I clear the cache again. – Ex0r Aug 24 '20 at 14:46
  • @Ex0r try moving your site to a different environment/server or try it locally on your PC and see if the issue persists. Seems to me there is probably some external (non Drupal) caching mechanism that is responsible for what you're experiencing. – No Sssweat Aug 24 '20 at 15:02
  • It does persist, yes. On both my local dev environment and my production environment. – Ex0r Aug 25 '20 at 0:52
  • Try to replicare behavior with fresh Drupal install and no custom nor contrib modules other than to alter view. – No Sssweat Aug 25 '20 at 0:56

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.