0

I want to remove certain entities from the view results (Views core module), before the view's pager is generated.

By now I am doing it in hook_views_post_execute(). The problem here is, that the number of results on the view pages is different then. There should be 10 results per page and not 7, 8 or 6 etc.

I do not want to create a views filter, which would require manual interaction from the user inside the views interface in the webbrowser.

I have tried also hook_views_query_alter(). The problem here is, that my module's class methods are processing entity id's (node id's in particular). It might would work with the SQL NOT IN statement, but this is probably a problem for bigger site's and a huge performance impact. Since I need to iterate over all entity id's from the site and check access then.

My questions:

  • Is there any method inside the Views module, which allows me to remove the entity id's from the view results or shall I really use hook_views_query_alter()?
  • Also - if it comes to the hook_views_X methods - isn't there any more elegant class method or event? Since hooks are a mostly deprecated way in Drupal 8.

Thanks for your attention!

1 Answer 1

0

You need to alter the query in one way or another, that is the only way the pager can work.

Sounds like you are using nodes, then you can use the node access grant system. Basically, it allows you to "index" the access checks and assign attributes to the nodes, like the group they belong to. And then there is a second hook, that allows you to define what groups (or whatever it is in your case) the user has access to. This will then be combined into a query for you.

Be aware that this is one of the most complicated systems in Drupal, but if you can't do the access check directly as part of the query (like checking the value of a certain field), then this is the only way.

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.