RouteSubscriber.php in Organic groups 8
File
src/Routing/RouteSubscriber.php
View source
<?php
declare (strict_types=1);
namespace Drupal\og\Routing;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\og\Event\OgAdminRoutesEvent;
use Drupal\og\Event\OgAdminRoutesEventInterface;
use Drupal\og\OgAccess;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
use StringTranslationTrait;
protected $entityTypeManager;
protected $routeProvider;
protected $eventDispatcher;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteProviderInterface $route_provider, EventDispatcherInterface $event_dispatcher) {
$this->entityTypeManager = $entity_type_manager;
$this->routeProvider = $route_provider;
$this->eventDispatcher = $event_dispatcher;
}
protected function alterRoutes(RouteCollection $collection) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if (!$entity_type
->hasLinkTemplate('canonical')) {
continue;
}
if (!($og_admin_path = $entity_type
->getLinkTemplate('og-admin-routes'))) {
continue;
}
$entity_type_id = $entity_type
->id();
$route_name = "entity.{$entity_type_id}.og_admin_routes";
$route = new Route($og_admin_path);
$route
->addDefaults([
'_controller' => '\\Drupal\\og\\Controller\\OgAdminRoutesController::overview',
'_title' => 'Group management',
])
->addRequirements([
'_og_user_access_group' => OgAccess::ADMINISTER_GROUP_PERMISSION,
])
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
])
->setOption('_og_entity_type_id', $entity_type_id)
->setOption('_admin_route', TRUE);
$collection
->add($route_name, $route);
$this
->createRoutesFromEventSubscribers($og_admin_path, $entity_type_id, $collection);
}
}
protected function createRoutesFromEventSubscribers($og_admin_path, $entity_type_id, RouteCollection $collection) {
$event = new OgAdminRoutesEvent();
$this->eventDispatcher
->dispatch(OgAdminRoutesEventInterface::EVENT_NAME, $event);
foreach ($event
->getRoutes($entity_type_id) as $name => $route_info) {
$parent_route_name = "entity.{$entity_type_id}.og_admin_routes.{$name}";
$parent_path = $og_admin_path . '/' . $route_info['path'];
$this
->addRoute($collection, $parent_route_name, $parent_path, $route_info);
}
}
protected function addRoute(RouteCollection $collection, $route_name, $path, array $route_info) {
$route = new Route($path);
$route
->addDefaults($route_info['defaults'])
->addRequirements($route_info['requirements'])
->addOptions($route_info['options']);
$collection
->add($route_name, $route);
}
public static function getSubscribedEvents() {
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
100,
];
return $events;
}
}