You are here

class RouteSubscriber in View Modes Display 8.2

Subscriber for View Mode Display routes.

Hierarchy

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

Expanded class hierarchy of RouteSubscriber

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

File

src/Routing/RouteSubscriber.php, line 16

Namespace

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

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

  /**
   * EntityDisplayRepository.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $entityDisplayRepository;

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

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      if ($viewModes = $this->entityDisplayRepository
        ->getViewModes($entity_type_id)) {
        if ($route = $this
          ->getPreviewList($entity_type)) {
          $collection
            ->add("entity.{$entity_type_id}.vmd_preview_list", $route);
        }
        if ($route = $this
          ->getPreviewRenderRoute($entity_type)) {
          $collection
            ->add("entity.{$entity_type_id}.vmd_preview_render", $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 getPreviewList(EntityTypeInterface $entity_type) {
    if ($link_template = $entity_type
      ->getLinkTemplate('vmd-preview-list')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($link_template);
      $route
        ->addDefaults([
        '_controller' => '\\Drupal\\view_modes_display\\Controller\\PreviewController::previewList',
        '_title' => 'Available View Mode Previews',
        'entity_type' => $entity_type_id,
        'view_mode' => 'all',
      ])
        ->addRequirements([
        '_permission' => 'preview view modes',
      ])
        ->setOption('_admin_route', TRUE)
        ->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 getPreviewRenderRoute(EntityTypeInterface $entity_type) {
    if ($link_template = $entity_type
      ->getLinkTemplate('vmd-preview-list')) {
      $entity_type_id = $entity_type
        ->id();
      $route = new Route($link_template);
      $route
        ->addDefaults([
        '_controller' => '\\Drupal\\view_modes_display\\Controller\\PreviewController::previewEntity',
        '_title' => 'View Mode Preview',
        'entity_type' => $entity_type_id,
      ])
        ->addRequirements([
        '_permission' => 'preview view modes',
      ])
        ->setOption('parameters', [
        $entity_type_id => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ]);
      return $route;
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$entityDisplayRepository protected property EntityDisplayRepository.
RouteSubscriber::$entityTypeManager protected property The entity type manager.
RouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::getPreviewList protected function Gets the entity load route.
RouteSubscriber::getPreviewRenderRoute protected function Gets the entity render 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 RouteSubscriber object.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1