RouteSubscriber.php in Lightning Core 8
File
src/Routing/RouteSubscriber.php
View source
<?php
namespace Drupal\lightning_core\Routing;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\lightning_core\Controller\FieldUiTitleController;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriber extends RouteSubscriberBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function alterRoutes(RouteCollection $collection) {
foreach ($this->entityTypeManager
->getDefinitions() as $id => $entity_type) {
if ($entity_type
->get('field_ui_base_route') == NULL) {
continue;
}
$this
->setBundleAsTitle("entity.{$id}.field_ui_fields", $collection);
$this
->setBundleAsTitle("entity.entity_view_display.{$id}.default", $collection);
$this
->setBundleAsTitle("entity.entity_view_display.{$id}.view_mode", $collection);
$this
->setBundleAsTitle("entity.entity_form_display.{$id}.default", $collection);
$this
->setBundleAsTitle("entity.entity_form_display.{$id}.form_mode", $collection);
}
}
public static function isViewing(EntityInterface $entity, RouteMatchInterface $route_match = NULL) {
$route_match = $route_match ?: \Drupal::routeMatch();
$entity_type = $entity
->getEntityTypeId();
return $route_match
->getRouteName() == "entity.{$entity_type}.canonical" && $route_match
->getRawParameter($entity_type) == $entity
->id();
}
protected function setBundleAsTitle($route_name, RouteCollection $collection) {
$route = $collection
->get($route_name);
if ($route) {
$route
->setDefault('_title_callback', FieldUiTitleController::class . '::bundle');
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
-110,
];
return $events;
}
}