You are here

class RouteSubscriber in Entity Usage 8.3

Same name and namespace in other branches
  1. 8.4 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber
  2. 8 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber
  3. 8.2 src/Routing/RouteSubscriber.php \Drupal\entity_usage\Routing\RouteSubscriber

Registers a route for generic usage local tasks for entities.

Hierarchy

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
entity_usage.services.yml in ./entity_usage.services.yml
entity_usage.services.yml
1 service uses RouteSubscriber
entity_usage.route_subscriber in ./entity_usage.services.yml
Drupal\entity_usage\Routing\RouteSubscriber

File

src/Routing/RouteSubscriber.php, line 15

Namespace

Drupal\entity_usage\Routing
View 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) {

      // 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);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = [
      'onAlterRoutes',
      99,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$config protected property The config factory service.
RouteSubscriber::$entityTypeManager protected property The entity type manager service.
RouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase::getSubscribedEvents
RouteSubscriber::__construct public function Constructs a new RouteSubscriber object.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1