Created
February 3, 2022 19:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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