You are here

protected function RouteSubscriber::alterRoutes 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::alterRoutes()

Alters existing routes for a specific collection.

Parameters

\Symfony\Component\Routing\RouteCollection $collection: The route collection for adding routes.

Overrides RouteSubscriberBase::alterRoutes

File

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

Class

RouteSubscriber
Subscriber for Field UI routes.

Namespace

Drupal\wsdata_field\Routing

Code

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);
    }
  }
}