@@ -37,36 +37,28 @@ function advanced_search_default_sort_override_theme() {
3737}
3838
3939/**
40- * Implements hook_views_pre_view().
40+ * Implements hook_views_pre_build().
41+ *
42+ * Replaces hook_views_pre_view. This modifiees the live objects instead of the config array.
4143 */
42- function advanced_search_default_sort_override_views_pre_view (ViewExecutable $view, $display_id, array &$args ) {
44+ function advanced_search_default_sort_override_views_pre_build (ViewExecutable $view) {
4345 // Only run on views except for the admin UI.
4446 if (\Drupal::service('router.admin_context')->isAdminRoute()) {
4547 return;
4648 }
4749
48- // Flag to keep track of any changes we make.
49- $modified = FALSE;
50-
5150 $paramList = \Drupal::request()->query->all();
5251
5352 // Only do the processing if there is any active search.
5453 if (count($paramList) > 0) {
55- $sorts = $view->getHandlers('sort');
56-
57- // Iterate through sorts and remove if the "Default sort only" checkbox is checked.
58- foreach ($sorts as $key => $sort) {
59- if (!empty($sort['default_sort_only'])) {
60- unset($sorts[$key]);
61- $modified = TRUE;
54+ // Iterate through the LIVE instantiated sort objects.
55+ foreach ($view->sort as $id => $handler) {
56+ if (!empty($handler->options['default_sort_only'])) {
57+ // Unset the live object sort.
58+ unset($view->sort[$id]);
6259 }
6360 }
6461 }
65-
66- // Use the modified sorts only if we changed something.
67- if ($modified) {
68- $view->display_handler->overrideOption('sorts', $sorts);
69- }
7062}
7163
7264/**
0 commit comments