You are here

class RouteSubscriber in Schema.org configuration tool (RDF UI) 8

Subscriber for RDF UI routes.

Hierarchy

Expanded class hierarchy of RouteSubscriber

1 string reference to 'RouteSubscriber'
rdfui.services.yml in ./rdfui.services.yml
rdfui.services.yml
1 service uses RouteSubscriber
rdfui.subscriber in ./rdfui.services.yml
Drupal\rdfui\Routing\RouteSubscriber

File

src/Routing/RouteSubscriber.php, line 15

Namespace

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

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

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

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[RoutingEvents::ALTER] = array(
      'onAlterRoutes',
      -100,
    );
    return $events;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->manager
      ->getDefinitions() as $entity_type_id => $entity_type) {
      if ($route_name = $entity_type
        ->get('field_ui_base_route')) {

        // Try to get the route from the current collection.
        if (!($entity_route = $collection
          ->get($route_name))) {
          continue;
        }
        $path = $entity_route
          ->getPath();
        $options = array();
        if (($bundle_entity_type = $entity_type
          ->getBundleEntityType()) && $bundle_entity_type !== 'bundle') {
          $options['parameters'][$bundle_entity_type] = array(
            'type' => 'entity:' . $bundle_entity_type,
          );

          // Special parameter used to easily recognize all Field UI routes.
          $options['_field_ui'] = TRUE;
        }
        $defaults = array(
          'entity_type_id' => $entity_type_id,
        );

        // If the entity type has no bundles and it doesn't use {bundle} in its
        // admin path, use the entity type.
        if (strpos($path, '{bundle}') === FALSE) {
          $defaults['bundle'] = !$entity_type
            ->hasKey('bundle') ? $entity_type_id : '';
        }
        $route = new Route("{$path}/fields/rdf", array(
          '_form' => '\\Drupal\\rdfui\\Form\\FieldMappings',
          '_title' => 'Manage fields RDF',
        ) + $defaults, array(
          '_permission' => 'administer ' . $entity_type_id . ' fields',
        ), $options);
        $collection
          ->add("entity.{$entity_type_id}.rdf_ui_fields", $route);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$manager protected property The entity type manager.
RouteSubscriber::alterRoutes protected 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::__construct public function Constructs a RouteSubscriber object.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1