How can I limit query results in hook_views_pre_execute? I am developing a Drupal 8 website where I have a block view displayed for certain content types that shows related information from other content types. I need to dynamically limit query results based on the moderation state of the node being visited (as opposed to the content in the view). My plan was to grab the node's moderation state from the page context and use that to limit the results of the view. However, the setLimit function seems to have no effect on query results:
function myModule_views_pre_execute(ViewExecutable $view) {
  // Only for my_view view.
  if ($view-&gt;id() == 'my_view') {
    $list_node = Drupal::request()-&gt;attributes-&gt;get('node');
    if($list_node-&gt;moderation_state-&gt;value == &quot;approved&quot;){

    }else{
      dpm(&quot;node is not approved, setting limit to 0&quot;);
      $view-&gt;query-&gt;setLimit(0);
    }   
  }

UPDATE
I played around with trying different things in different hooks, inspecting the actual query generated via the view editing interface, as this exposes the &quot;LIMIT #&quot; expression. I learned 2 main things from that process:

Things can and will edit the query in other hooks. I found that putting the setLimit in hook_views_query_alter got overridden by some other module, and by the time hook_views_pre_execute came around, it would be changed from what I set it to earlier. However setting it in pre_execute is working fine.
Amazingly, calling setLimit(0) removes the LIMIT expression from the query entirely! However, any other value seems to work fine.

Is this expected behavior? Does Drupal prohibit setting the limit for a query to 0? Or should I be logging an issue somewhere?
No idea why setLimit(0) doesn't work as expected, but an alternative is to act as though you had limited the results to 0. When you add a contextual filter to a view, one of the options for When the filter value is NOT available is to Display contents of &quot;No results found&quot;. This seems to be handled by ArgumentPluginBase::defaultEmpty(), which looks like
public function defaultEmpty() {
  // We return with no query; this will force the empty text.
  $this-&gt;view-&gt;built = TRUE;
  $this-&gt;view-&gt;executed = TRUE;
  $this-&gt;view-&gt;result = [];
  return FALSE;
}

If you drop this snippet into hook_views_query_alter() it appears to work exactly the same! I'd wager that you could drop it into any of the views hooks, actually, and it would have the same effect.
EDIT: Whoops, forgot to say you'd want to replace $this-&gt;view with $view if you're inside of the hooks.