You are here

class RouteSubscriber in Automatic Entity Label 8

Same name and namespace in other branches
  1. 8.3 src/Routing/RouteSubscriber.php \Drupal\auto_entitylabel\Routing\RouteSubscriber
  2. 8.2 src/Routing/RouteSubscriber.php \Drupal\auto_entitylabel\Routing\RouteSubscriber

Subscriber for auto_entitylabel routes.

Hierarchy

  • class \Drupal\Core\Routing\RouteSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RouteSubscriber

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

File

src/Routing/RouteSubscriber.php, line 15

Namespace

Drupal\auto_entitylabel\Routing
View source
class RouteSubscriber extends RouteSubscriberBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new RouteSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager) {
    $this->entityTypeManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      if ($route = $this
        ->getEntityLabelRoute($entity_type)) {
        $collection
          ->add("entity.{$entity_type_id}.auto_label", $route);
      }
    }
  }

  /**
   * Gets the Entity Auto Label route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getEntityLabelRoute(EntityTypeInterface $entity_type) {
    if ($route_load = $entity_type
      ->getLinkTemplate('auto-label')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($route_load);
      $route
        ->addDefaults([
        '_form' => '\\Drupal\\auto_entitylabel\\Form\\AutoEntityLabelForm',
        '_title' => 'Automatic entity label',
      ])
        ->addRequirements([
        '_permission' => 'administer ' . $entity_type_id . ' labels',
      ])
        ->setOption('_admin_route', TRUE)
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);
      return $route;
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$entityTypeManager protected property The entity type manager.
RouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::getEntityLabelRoute protected function Gets the Entity Auto Label route.
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