You are here

class DefaultRouteSubscriber in Form mode manager 8

Subscriber for form_mode_manager routes.

Hierarchy

Expanded class hierarchy of DefaultRouteSubscriber

1 string reference to 'DefaultRouteSubscriber'
form_mode_manager.services.yml in ./form_mode_manager.services.yml
form_mode_manager.services.yml
1 service uses DefaultRouteSubscriber
form_mode_manager.default_routes_subscriber in ./form_mode_manager.services.yml
Drupal\form_mode_manager\Routing\EventSubscriber\DefaultRouteSubscriber

File

src/Routing/EventSubscriber/DefaultRouteSubscriber.php, line 15

Namespace

Drupal\form_mode_manager\Routing\EventSubscriber
View source
class DefaultRouteSubscriber extends RouteSubscriberBase {

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

  /**
   * The entity display repository.
   *
   * @var \Drupal\form_mode_manager\FormModeManagerInterface
   */
  protected $formModeManager;

  /**
   * The Routes Manager Plugin.
   *
   * @var \Drupal\form_mode_manager\EntityRoutingMapManager
   */
  protected $entityRoutingMap;

  /**
   * Constructs a new RouteSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity type manager.
   * @param \Drupal\form_mode_manager\FormModeManagerInterface $form_mode_manager
   *   The form mode manager.
   * @param \Drupal\form_mode_manager\EntityRoutingMapManager $plugin_routes_manager
   *   Plugin EntityRoutingMap to retrieve entity form operation routes.
   */
  public function __construct(EntityTypeManagerInterface $entity_manager, FormModeManagerInterface $form_mode_manager, EntityRoutingMapManager $plugin_routes_manager) {
    $this->entityTypeManager = $entity_manager;
    $this->formModeManager = $form_mode_manager;
    $this->entityRoutingMap = $plugin_routes_manager;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    $form_modes_definitions = $this->formModeManager
      ->getAllFormModesDefinitions();
    foreach (array_keys($form_modes_definitions) as $entity_type_id) {
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      $this
        ->enhanceDefaultRoutes($collection, $entity_type
        ->id());
    }
  }

  /**
   * Enhance existing entity operation routes (add_page, add_form, edit_form).
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection to retrieve parent entity routes.
   * @param string $entity_type_id
   *   The ID of the entity type.
   */
  protected function enhanceDefaultRoutes(RouteCollection $collection, $entity_type_id) {
    $this
      ->enhanceDefaultAddPageRoutes($collection, $entity_type_id);
    $this
      ->enhanceDefaultAddRoutes($collection, $entity_type_id);
    $this
      ->enhanceDefaultEditRoutes($collection, $entity_type_id);
  }

  /**
   * Enhance entity operation routes add_page.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection to retrieve parent entity routes.
   * @param string $entity_type_id
   *   The ID of the entity type.
   */
  protected function enhanceDefaultAddPageRoutes(RouteCollection $collection, $entity_type_id) {
    $entity_add_page = $this->entityRoutingMap
      ->createInstance($entity_type_id, [
      'entityTypeId' => $entity_type_id,
    ])
      ->getOperation('add_page');
    if ($entity_add_page && ($route = $collection
      ->get($entity_add_page))) {
      $collection
        ->add($entity_add_page, $this
        ->routeEnhancer($route, $entity_type_id));
    }
  }

  /**
   * Enhance entity operation routes add.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection to retrieve parent entity routes.
   * @param string $entity_type_id
   *   The ID of the entity type.
   */
  protected function enhanceDefaultAddRoutes(RouteCollection $collection, $entity_type_id) {
    $entity_route_name = $this->entityRoutingMap
      ->createInstance($entity_type_id, [
      'entityTypeId' => $entity_type_id,
    ])
      ->getOperation('add_form');
    if ($route = $collection
      ->get($entity_route_name)) {
      $collection
        ->add($entity_route_name, $this
        ->routeEnhancer($route, $entity_type_id));
    }
  }

  /**
   * Enhance entity operation routes edit.
   *
   * @param \Symfony\Component\Routing\RouteCollection $collection
   *   The route collection to retrieve parent entity routes.
   * @param string $entity_type_id
   *   The ID of the entity type.
   */
  protected function enhanceDefaultEditRoutes(RouteCollection $collection, $entity_type_id) {
    $entity_route_name = $this->entityRoutingMap
      ->createInstance($entity_type_id, [
      'entityTypeId' => $entity_type_id,
    ])
      ->getOperation('edit_form');
    if ($route = $collection
      ->get($entity_route_name)) {
      $collection
        ->add($entity_route_name, $this
        ->routeEnhancer($route, $entity_type_id));
    }
  }

  /**
   * Add required parameters on route basis.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route object of entity.
   * @param string $entity_type_id
   *   The ID of the entity type.
   *
   * @return \Symfony\Component\Routing\Route
   *   The route enhanced.
   */
  public function routeEnhancer(Route $route, $entity_type_id) {
    $route
      ->setRequirement('_permission', "use {$entity_type_id}.default form mode");
    return $route;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DefaultRouteSubscriber::$entityRoutingMap protected property The Routes Manager Plugin.
DefaultRouteSubscriber::$entityTypeManager protected property The entity type manager service.
DefaultRouteSubscriber::$formModeManager protected property The entity display repository.
DefaultRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
DefaultRouteSubscriber::enhanceDefaultAddPageRoutes protected function Enhance entity operation routes add_page.
DefaultRouteSubscriber::enhanceDefaultAddRoutes protected function Enhance entity operation routes add.
DefaultRouteSubscriber::enhanceDefaultEditRoutes protected function Enhance entity operation routes edit.
DefaultRouteSubscriber::enhanceDefaultRoutes protected function Enhance existing entity operation routes (add_page, add_form, edit_form).
DefaultRouteSubscriber::routeEnhancer public function Add required parameters on route basis.
DefaultRouteSubscriber::__construct public function Constructs a new RouteSubscriber object.
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1