#1: To alter Query parameter value in the url of View page: redirect event subscriber approach used
<?php
namespace Drupal\custom_module;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Url;
/**
* Execute to changes query parameter value in the url of view page.
*/
class RedirectSubscriber implements EventSubscriberInterface {
/**
* Current Route.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $route;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a RedirectSubscriber object.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
* The route match.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack.
*/
public function __construct(RouteMatchInterface $current_route_match, RequestStack $requestStack) {
$this->route = $current_route_match;
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public function redirect(GetResponseEvent $event) {
$route_name = $this->route->getRouteName();
// Get category name
$term_name = $this->requestStack->getCurrentRequest()->query->get('category_name');
// Get term id from category name
$ary = preg_split('/[()]/', $term_name);
$tid = $ary[1];
if ($route_name == 'view.speaker.page_2' && isset($term_name)) {
if (isset($tid)) {
// Get all paramters page
$params = $this->requestStack->getCurrentRequest()->query->All();
// Alter the category_name query parameter value
if ($params['category_name']) {
$params['category_name'] = $tid;
}
// Set redirect url & pass all parameters to it
$redirect_url = Url::fromRoute('view.view_bulk_test.page_2')->setOptions(['query' => $params])->toString();
if ($redirect_url) {
// Pass Redirect URL.
$event->setResponse(new RedirectResponse($redirect_url, 301));
}
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = ['Redirect', 20];
return $events;
}
}
File path: : module_name/src/RedirectSubscriber.php
Define: module_name.services.yml (inside custom module)
services:
module_name.redirect_subscriber:
class: Drupal\module_name\RedirectSubscriber
arguments: ['@current_route_match', '@request_stack']
tags:
- { name: 'event_subscriber' }
In this scenario query parameter whose value altered is autocomplete taxonomy referenced type field & if tid passed then error 'll display:
There are no taxonomy terms matching "2".
To fix it: Used hook hook_views_pre_view():
use Drupal\taxonomy\Entity\Term;
function module_name_views_pre_view(ViewExecutable $view, string $display_id, array $args) {
if (($view->id() == 'speaker')) {
$exposedInput = $view->getExposedInput();
// Get term id from category name
$tid = $exposedInput['category_name'];
if(isset($tid)){
// Load term object from term id
$term = Term::load($tid);
if(isset($term)){
// Set value of filter in required format
$val = $term->label().' '.'('.$tid.')';
$exposedInput['category_name'] = $val;
}
// Set the exposed filter value in the view
$view->setExposedInput($exposedInput);
}
}
}
#2: Query paramter name in the url of view page can be altered by the view UI.
Steps:
Go to the required view and edit the view
Click the filter & filter settings 'll open in the modal
You will find 'Filter identifier' field , here you can specify the required parameter name.
