class RouteSubscriber in Entity Usage 8
Same name and namespace in other branches
- 8.4 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber
- 8.2 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber
- 8.3 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber
Registers a route for generic usage local tasks for entities.
Hierarchy
- class \Drupal\Core\Routing\RouteSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface- class \Drupal\entity_usage\Routing\RouteSubscriber
 
Expanded class hierarchy of RouteSubscriber
1 string reference to 'RouteSubscriber'
1 service uses RouteSubscriber
File
- src/Routing/ RouteSubscriber.php, line 16 
Namespace
Drupal\entity_usage\RoutingView source
class RouteSubscriber extends RouteSubscriberBase {
  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;
  /**
   * Constructs a new RouteSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The config factory service.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config) {
    $this->entityTypeManager = $entity_manager;
    $this->config = $config;
  }
  /**
   * {@inheritdoc}
   */
  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);
      }
    }
  }
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = [
      'onAlterRoutes',
      99,
    ];
    return $events;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| RouteSubscriber:: | protected | property | The config factory service. | |
| RouteSubscriber:: | protected | property | The entity type manager service. | |
| RouteSubscriber:: | protected | function | Alters existing routes for a specific collection. Overrides RouteSubscriberBase:: | |
| RouteSubscriber:: | public static | function | Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase:: | |
| RouteSubscriber:: | public | function | Constructs a new RouteSubscriber object. | |
| RouteSubscriberBase:: | public | function | Delegates the route altering to self::alterRoutes(). | 1 | 
