You are here

class RouteSubscriber in Devel 8.3

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

Subscriber for Devel routes.

Hierarchy

Expanded class hierarchy of RouteSubscriber

See also

\Drupal\devel\Controller\EntityDebugController

\Drupal\devel\Plugin\Derivative\DevelLocalTask

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

File

src/Routing/RouteSubscriber.php, line 18

Namespace

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

  /**
   * The entity type manager service.
   *
   * @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
        ->getEntityLoadRoute($entity_type)) {
        $collection
          ->add("entity.{$entity_type_id}.devel_load", $route);
      }
      if ($route = $this
        ->getEntityRenderRoute($entity_type)) {
        $collection
          ->add("entity.{$entity_type_id}.devel_render", $route);
      }
      if ($route = $this
        ->getEntityTypeDefinitionRoute($entity_type)) {
        $collection
          ->add("entity.{$entity_type_id}.devel_definition", $route);
      }
    }
  }

  /**
   * Gets the entity load route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getEntityLoadRoute(EntityTypeInterface $entity_type) {
    if ($devel_load = $entity_type
      ->getLinkTemplate('devel-load')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($devel_load);
      $route
        ->addDefaults([
        '_controller' => '\\Drupal\\devel\\Controller\\EntityDebugController::entityLoad',
        '_title' => 'Devel Load',
      ])
        ->addRequirements([
        '_permission' => 'access devel information',
      ])
        ->setOption('_admin_route', TRUE)
        ->setOption('_devel_entity_type_id', $entity_type_id)
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);
      return $route;
    }
  }

  /**
   * Gets the entity render route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getEntityRenderRoute(EntityTypeInterface $entity_type) {
    if ($devel_render = $entity_type
      ->getLinkTemplate('devel-render')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($devel_render);
      $route
        ->addDefaults([
        '_controller' => '\\Drupal\\devel\\Controller\\EntityDebugController::entityRender',
        '_title' => 'Devel Render',
      ])
        ->addRequirements([
        '_permission' => 'access devel information',
      ])
        ->setOption('_admin_route', TRUE)
        ->setOption('_devel_entity_type_id', $entity_type_id)
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);
      return $route;
    }
  }

  /**
   * Gets the entity type definition route.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   The generated route, if available.
   */
  protected function getEntityTypeDefinitionRoute(EntityTypeInterface $entity_type) {
    if ($devel_definition = $entity_type
      ->getLinkTemplate('devel-definition')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($devel_definition);
      $route
        ->addDefaults([
        '_controller' => '\\Drupal\\devel\\Controller\\EntityDebugController::entityTypeDefinition',
        '_title' => 'Entity type definition',
      ])
        ->addRequirements([
        '_permission' => 'access devel information',
      ])
        ->setOption('_admin_route', TRUE)
        ->setOption('_devel_entity_type_id', $entity_type_id)
        ->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 service.
RouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::getEntityLoadRoute protected function Gets the entity load route.
RouteSubscriber::getEntityRenderRoute protected function Gets the entity render route.
RouteSubscriber::getEntityTypeDefinitionRoute protected function Gets the entity type definition 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