The view has on offset based on a value set in the Basic page node.

function domain_config_bu_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
    if($node = \Drupal::routeMatch()->getParameter('node')) {
      if(... // Node has certain field value set) {

On a page request i can get the current node object by routeMatch() functionality.
But views has ajax enabled, and on ajax callback routeMatch() does not return the node object. Wich is normal. But how can i get the current node object/ nid in an ajax callback so the offset is respected?

1 Answer 1


Check if request is ajax route (it is not always '.ajax', log it to be certain). Get referer and retrieve the url object. The url object contains the parameters to get the current node.

// Get route
$route_name = \Drupal::routeMatch()->getRouteName();

// On ajax request get route_name of page.
if (strpos($route_name , '.ajax') !== FALSE) {
    $previousUrl = \Drupal::request()->server->get('HTTP_REFERER'); 
    $fake_request = Request::create($previousUrl);
    $url_object = \Drupal::service('path.validator')->getUrlIfValid($fake_request->getRequestUri());
    // Use $url_object to retreive your node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.