Skip to content

Instantly share code, notes, and snippets.

@jenitehan
Created February 3, 2022 19:44
Show Gist options
  • Save jenitehan/761510a39924fd2232ccc18e344cf47a to your computer and use it in GitHub Desktop.
Save jenitehan/761510a39924fd2232ccc18e344cf47a to your computer and use it in GitHub Desktop.
Set Drupal group parameter on a views page route with a group ID contextual filter.
<?php
namespace Drupal\my_module\EventSubscriber;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\RouteCollection;
/**
* My module route subscriber.
*/
class MyModuleRouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('view.my_view.page_1')) {
$parameters = $route->getOption('parameters');
if (!$parameters) {
$parameters = [];
}
$parameters['group']['type'] = 'entity:group';
$parameters['group']['converter'] = 'paramconverter.entity';
$route->setOption('parameters', $parameters);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
// Use a lower priority than \Drupal\views\EventSubscriber\RouteSubscriber
// to ensure the requirement will be added to its routes.
$events[RoutingEvents::ALTER] = ['onAlterRoutes', -300];
return $events;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment