I want to add a simple filter to an existing view in Drupal 9. It appears that I can achieve this with hook_views_pre_view()
. In every example I can find, including all the "Similar questions" links that come up when entering the title of this post, I see that $view->add_item()
is used. See: Add views exposed filter programmatically
However, when I try to run this code:
function sbn_views_pre_view($view, $display_id, array $args) {
if ($view->id() == 'draft_moderation_state') {
// Get array of draft nids
$state = 'draft';
$results = sbn_get_all_nodes_in_moderation_state($state);
// There is no draft_moderation_state filter so we have to add it
$view->add_item(
$view->current_display,
'filter',
'node',
'nid',
array(
'operator' => '=',
'value' => '59',
'group' => 1
)
);
}
}
I get this error:
Call to undefined method Drupal\views\ViewExecutable::add_item()
What is the command to add filter in hook_views_pre_view()
in Drupal 9?