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
- class \Drupal\view_modes_display\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\view_modes_display\RoutingView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RouteSubscriber:: |
protected | property | EntityDisplayRepository. | |
RouteSubscriber:: |
protected | property | The entity type manager. | |
RouteSubscriber:: |
protected | function |
Alters existing routes for a specific collection. Overrides RouteSubscriberBase:: |
|
RouteSubscriber:: |
protected | function | Gets the entity load route. | |
RouteSubscriber:: |
protected | function | Gets the entity render route. | |
RouteSubscriber:: |
public static | function |
Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase:: |
|
RouteSubscriber:: |
public | function | Constructs a RouteSubscriber object. | |
RouteSubscriberBase:: |
public | function | Delegates the route altering to self::alterRoutes(). | 1 |