I know the question is old, but hopefully this will help someone else. After working extensively to use hook_views_pre_view, I finally found the solution by switching to hook_views_pre_build. This code allowed me to make the changes I needed.
/**
* Implements hook_views_pre_build().
*/
function msul_newsletter_helper_views_pre_build(Drupal\views\ViewExecutable $view){
/*
* to check the format of the argument for setExposedInput, view the results
* of getExposedInput on your view WHICH HAS HAD EXPOSED FILTERS SUBMITTED.
* I use dpm($view->getExposedInput());
*/
//always check the view ID so you don't affect all views.
if($view->id()=='newsletter'){
//check display id if you don't want to affect all displays in view.
if( $view->current_display == 'page_2') {
if(empty($view->getExposedInput())){
//in my case I get $editionList from a database query.
// because I am putting this as a default, I don't worry about other input,
// but if you are concerned about other input, modify the array
// from $view->getExposedInput() instead of just placing the information.
$view->setExposedInput(['newsletter_edition_public_filter'=>
['edition'=>$editionList]
]);
}
}
//in this case, I affect 2 displays.
if($view->current_display == 'page_4'
|| $view->current_display == 'block_1'
){
if(empty($view->getExposedInput())){
//in my case I get $latestEdition from a database query.
$view->setExposedInput(['edition'=>['edition'=>[$latestEdition]]]);
}
}
}
}
hook_views_pre_execute()
orhook_views_pre_render()
, or use a preprocess hook and alter the View executable object. api.drupal.org/api/drupal/…