You are here

class RouteSubscriber in Replicate UI 8

Hierarchy

  • class \Drupal\replicate_ui\RouteSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RouteSubscriber

1 file declares its use of RouteSubscriber
RouteSubscriberTest.php in tests/src/Unit/RouteSubscriberTest.php
1 string reference to 'RouteSubscriber'
replicate_ui.services.yml in ./replicate_ui.services.yml
replicate_ui.services.yml
1 service uses RouteSubscriber
replicate_ui.route_subscriber in ./replicate_ui.services.yml
\Drupal\replicate_ui\RouteSubscriber

File

src/RouteSubscriber.php, line 13

Namespace

Drupal\replicate_ui
View source
class RouteSubscriber implements EventSubscriberInterface {

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Creates a new RouteSubscriber instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entityTypeManager;
    $this->configFactory = $config_factory;
  }
  public function onRouteBuild(RouteBuildEvent $event) {
    $config = $this->configFactory
      ->get('replicate_ui.settings');
    $collection = $event
      ->getRouteCollection();
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type instanceof ContentEntityTypeInterface && in_array($entity_type_id, (array) $config
        ->get('entity_types')) && $entity_type
        ->hasLinkTemplate('canonical')) {
        $base_path = $entity_type
          ->getLinkTemplate('canonical');
        $path = $base_path . '/replicate';
        $options = [
          'parameters' => [
            $entity_type_id => [
              'type' => 'entity:' . $entity_type_id,
            ],
          ],
        ];
        if ($entity_type_id == 'node') {

          // We delegate the decision of using the admin theme to
          // \Drupal\node\EventSubscriber\NodeAdminRouteSubscriber.
          $options['_node_operation_route'] = TRUE;
        }
        else {

          // Inherit admin route status from the edit route, if it exists.
          $route_name = "entity.{$entity_type_id}.edit_form";
          if (($edit_route = $collection
            ->get($route_name)) && $edit_route
            ->getOption('_admin_route')) {
            $options['_admin_route'] = TRUE;
          }
        }
        $defaults = [
          '_entity_form' => "{$entity_type_id}.replicate",
          'entity_type_id' => $entity_type_id,
        ];
        $requirements = [
          '_replicate_access' => 'TRUE',
        ];
        $route = new Route($path, $defaults, $requirements, $options);
        $route_name = "entity.{$entity_type_id}.replicate";
        $collection
          ->add($route_name, $route);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[RoutingEvents::DYNAMIC][] = 'onRouteBuild';
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$configFactory protected property
RouteSubscriber::$entityTypeManager protected property
RouteSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RouteSubscriber::onRouteBuild public function
RouteSubscriber::__construct public function Creates a new RouteSubscriber instance.