You are here

class RouteSubscriber in Web Service Data 8

Same name and namespace in other branches
  1. 2.0.x modules/wsdata_field/src/Routing/RouteSubscriber.php \Drupal\wsdata_field\Routing\RouteSubscriber

Subscriber for Field UI routes.

Hierarchy

Expanded class hierarchy of RouteSubscriber

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

File

modules/wsdata_field/src/Routing/RouteSubscriber.php, line 14

Namespace

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

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

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

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($this->entityTypeManager
      ->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 = $entity_route
          ->getOptions();
        if ($bundle_entity_type = $entity_type
          ->getBundleEntityType()) {
          $options['parameters'][$bundle_entity_type] = [
            'type' => 'entity:' . $bundle_entity_type,
          ];
        }

        // Special parameter used to easily recognize all Field UI routes.
        $options['_field_ui'] = TRUE;
        $defaults = [
          '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/add-wsfield", [
          '_form' => '\\Drupal\\wsdata_field\\Form\\WSFieldAddFieldForm',
          '_title' => 'Add wsfield',
        ] + $defaults, [
          '_permission' => 'administer ' . $entity_type_id . ' wsfields',
        ], $options);
        $collection
          ->add("wsdata_field.field_wsfield_config_add_{$entity_type_id}", $route);
        $route = new Route("{$path}/fields/{field_config}/wsfield_config", [
          '_entity_form' => 'wsfield_config.edit',
        ] + $defaults, [
          '_permission' => 'administer ' . $entity_type_id . ' wsfields',
        ], $options);
        $collection
          ->add("entity.field_config.{$entity_type_id}_wsfield_edit_form", $route);
      }
    }
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
RouteSubscriber::$entityTypeManager 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