You are here

class RouteSubscriber in Lightning Core 8.4

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

Dynamically alters various routes.

Hierarchy

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

Expanded class hierarchy of RouteSubscriber

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

File

src/Routing/RouteSubscriber.php, line 16

Namespace

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

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

  /**
   * RouteSubscriber constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $id => $entity_type) {
      if ($entity_type
        ->get('field_ui_base_route') == NULL) {
        continue;
      }

      // The 'Manage fields' page.
      $this
        ->setBundleAsTitle("entity.{$id}.field_ui_fields", $collection);

      // The default view display under 'Manage display'.
      $this
        ->setBundleAsTitle("entity.entity_view_display.{$id}.default", $collection);

      // A customized view display under 'Manage display'.
      $this
        ->setBundleAsTitle("entity.entity_view_display.{$id}.view_mode", $collection);

      // The default form display under 'Manage display'.
      $this
        ->setBundleAsTitle("entity.entity_form_display.{$id}.default", $collection);

      // A customized form display under 'Manage display'.
      $this
        ->setBundleAsTitle("entity.entity_form_display.{$id}.form_mode", $collection);
    }
  }

  /**
   * Checks if we are currently viewing an entity at its canonical route.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   (optional) The current route match.
   *
   * @return bool
   *   TRUE if we are at the entity's canonical route, FALSE otherwise.
   */
  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();
  }

  /**
   * Sets FieldUiTitleController::bundle() as the title callback for a route.
   *
   * @param string $route_name
   *   The route name.
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The complete route collection containing the route to alter.
   */
  protected function setBundleAsTitle($route_name, RouteCollection $collection) {
    $route = $collection
      ->get($route_name);
    if ($route) {
      $route
        ->setDefault('_title_callback', FieldUiTitleController::class . '::bundle');
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();

    // We need to run after Field UI.
    $events[RoutingEvents::ALTER] = [
      'onAlterRoutes',
      -110,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$entityTypeManager protected property The entity type manager service.
RouteSubscriber::alterRoutes public function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
RouteSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase::getSubscribedEvents
RouteSubscriber::isViewing public static function Checks if we are currently viewing an entity at its canonical route.
RouteSubscriber::setBundleAsTitle protected function Sets FieldUiTitleController::bundle() as the title callback for a route.
RouteSubscriber::__construct public function RouteSubscriber constructor.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1