RouteSubscriber.php in RedHen CRM 8
File
modules/redhen_connection/src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\redhen_connection\Routing;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_manager) {
$this->entityTypeManager = $entity_manager;
}
protected function alterRoutes(RouteCollection $collection) {
foreach (\Drupal::service('redhen_connection.connections')
->getAllConnectionEntityTypes() as $entity_type_id => $entity_type) {
if (!($path = $entity_type
->getLinkTemplate('redhen_connection'))) {
continue;
}
$route = new Route($path);
$route
->addDefaults([
'_controller' => '\\Drupal\\redhen_connection\\Controller\\RedhenConnections::list',
'_title' => 'Connections',
])
->addRequirements([
'_custom_access' => '\\Drupal\\redhen_connection\\Controller\\RedhenConnections::access',
])
->setOption('_admin_route', TRUE)
->setOption('parameters', [
$entity_type_id => [
'type' => 'entity:' . $entity_type_id,
],
]);
$collection
->add("entity.{$entity_type_id}.redhen_connection", $route);
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
100,
];
return $events;
}
}