I’ve got a view, that needs to set the default value for an exposed filter based on a user entity field. This basically works:
function MYMODULE_views_pre_build(DrupalviewsViewExecutable $view) { if ($view->current_display !== 'MY_VIEW_DISPLAY') { return; } $exposedFilterValues = $view->getExposedInput(); if (!array_key_exists('MY_FIELD', $exposedFilterValues)) { $personalizedDefaultValue = $someUserEntity->getMyCustomDefaultFilterValue(); $view->setExposedInput(array_merge($exposedFilterValues, ['MY_FIELD' => $personalizedDefaultValue] ); $view->element['#cache']['tags'] = Cache::mergeTags($view->element['#cache']['tags'] ?? [], $someUserEntity->getCacheTags()); } }
BUT:
This views page also has a contextual filter filtering a completely different field. As soon as a contextual filter value is present in the URL, the code above is still executed, but it has no effect on the frontend. Without a contextual filter value, it works perfectly.
Why doesn’t the code for the exposed field work when a completely different field’s contextual value exists?