views – How can I limit a query’s results in Drupal 8’s 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->id() == 'my_view') {
    $list_node = Drupal::request()->attributes->get('node');
    if($list_node->moderation_state->value == "approved"){

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