You are here

protected function RouteSubscriber::alterRoutes in Entity Usage 8

Same name and namespace in other branches
  1. 8.4 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber::alterRoutes()
  2. 8.2 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber::alterRoutes()
  3. 8.3 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber::alterRoutes()

Alters existing routes for a specific collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection for adding routes.

Overrides RouteSubscriberBase::alterRoutes

File

src/Routing/RouteSubscriber.php, line 48

Class

RouteSubscriber
Registers a route for generic usage local tasks for entities.

Namespace

Drupal\entity_usage\Routing

Code

protected function alterRoutes(RouteCollection $collection) {
  $configured_types = $this->config
    ->get('entity_usage.settings')
    ->get('local_task_enabled_entity_types') ?: [];
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if ($entity_type instanceof ContentEntityTypeInterface && ($canonical = $entity_type
      ->getLinkTemplate('canonical'))) {
      if (!in_array($entity_type_id, $configured_types)) {
        continue;
      }
      $options = [
        '_admin_route' => TRUE,
        '_entity_usage_entity_type_id' => $entity_type_id,
        'parameters' => [
          $entity_type_id => [
            'type' => 'entity:' . $entity_type_id,
          ],
        ],
      ];
      $route = new Route($canonical . '/usage', [
        '_controller' => '\\Drupal\\entity_usage\\Controller\\LocalTaskUsageController::listUsageLocalTask',
        '_title_callback' => '\\Drupal\\entity_usage\\Controller\\LocalTaskUsageController::getTitleLocalTask',
      ], [
        '_permission' => 'access entity usage statistics',
        '_custom_access' => '\\Drupal\\entity_usage\\Controller\\LocalTaskUsageController::checkAccessLocalTask',
      ], $options);
      $collection
        ->add("entity.{$entity_type_id}.entity_usage", $route);
    }
  }
}