I need to conditionally pass contextual filter values to a view based on the current route and am attempting to do so in hook_views_pre_view. This is the result of using AJAX via views_infinite_scroll and losing the contextual filters that I initially pass in programmatically.
On the initial request, the following works great for getting the current route in hook_views_pre_view:
$current_route_match = \Drupal::getContainer()->get('current_route_match');
$current_route_match->getRouteName();
However, when I hit the 'Load more' button supplied by views_infinite_scroll, the route name of the current route match is 'views.ajax'. While I understand that this is technically the request's route, I need to know the original route, or from where the request was made.
After digging around, I noticed that $_SERVER['HTTP_REFERER'] had the right path, but the documentation says:
Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
false
as the first in an effort to not filter by any value. For some reason this worked on the initial page request but ended up evaluating tonull
on the subsequent AJAX requests, being ignored completely. This bug caused me to think that contextual filters - applied programmatically - did not persist through multiple requests. Luckily I was wrong :)