0

I have a View, it is ajax View. There are exposed filters (with checkboxes). When I select a checkbox \Drupal::routeMatch()->getRouteName() shows general route name: views.ajax

I need to change view mode in the view:

function entity_product_admin_entity_view_mode_alter(&$view_mode, \Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'product' && \Drupal::routeMatch()->getRouteName() == 'views.ajax') {
    $view_mode = 'product_card_full_width';
  }
}

But in case Drupal::routeMatch()->getRouteName() == 'views.ajax' $view_mode will change on all ajax requests, but I need it to change only on a specific field of the form.

2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Sep 14, 2022 at 20:25
  • @Community Done. Sep 14, 2022 at 20:32

1 Answer 1

0

This hook seems to be not very useful in a Views context. You don't get much information, not even the view name. In an Ajax request Views removes any POST data from the request before executing the View.

You would need to save the view name (or any other POST data) at the start of the request in an event subscriber:

  public function onKernelRequest(RequestEvent $event) {
    $request = $event->getRequest();
    $view_name = $request->request->get('view_name');
    $request->attributes->set('view_name', $view_name);
  }

Use drush gen service:event-subscriber to generate the scaffolding.

Then the hook can get the view name:

$request = \Drupal::request();
$view_name = $request->attributes->get('view_name');

Your Answer

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

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