You are here

protected function RouteSubscriber::alterRoutes in Entity Usage 8.2

Same name and namespace in other branches
  1. 8.4 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber::alterRoutes()
  2. 8 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 47

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) {

    // We prefer the canonical template, but we also allow edit-form templates
    // on entities that don't have canonical (like views, etc).
    if ($entity_type
      ->hasLinkTemplate('canonical')) {
      $template = $entity_type
        ->getLinkTemplate('canonical');
    }
    elseif ($entity_type
      ->hasLinkTemplate('edit-form')) {
      $template = $entity_type
        ->getLinkTemplate('edit-form');
    }
    if (empty($template) || !in_array($entity_type_id, $configured_types, TRUE)) {
      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($template . '/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);
  }
}