You are here

protected function RouteSubscriber::alterRoutes in Entity Extra Field 8

Same name and namespace in other branches
  1. 2.0.x modules/entity_extra_field_ui/src/Routing/RouteSubscriber.php \Drupal\entity_extra_field_ui\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/entity_extra_field_ui/src/Routing/RouteSubscriber.php, line 38

Class

RouteSubscriber
Define the route subscriber.

Namespace

Drupal\entity_extra_field_ui\Routing

Code

protected function alterRoutes(RouteCollection $collection) {
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $definition) {
    $base_route_name = $definition
      ->get('field_ui_base_route');
    if (!isset($base_route_name)) {
      continue;
    }
    $entity_route = $collection
      ->get($base_route_name);
    if (!isset($entity_route)) {
      continue;
    }
    $entity_path = $entity_route
      ->getPath();
    $entity_options = $entity_route
      ->getOptions();
    $entity_bundle_type = $definition
      ->getBundleEntityType();
    if (isset($entity_bundle_type)) {
      $entity_options['parameters'][$entity_bundle_type] = [
        'type' => 'entity:' . $entity_bundle_type,
      ];
    }
    $route = new Route("{$entity_path}/extra-fields");
    $route
      ->setDefaults([
      '_title' => 'Manage extra fields',
      '_entity_list' => 'entity_extra_field',
      'entity_type_id' => $entity_type_id,
    ])
      ->setRequirements([
      '_permission' => 'administer entity extra field',
    ])
      ->setOptions($entity_options);
    $collection
      ->add("entity.{$entity_type_id}.extra_fields", $route);
    $route = new Route("{$entity_path}/extra-fields/add");
    $route
      ->setDefaults([
      '_title' => 'Add extra field',
      '_entity_form' => 'entity_extra_field.add',
      'entity_type_id' => $entity_type_id,
    ])
      ->setRequirements([
      '_permission' => 'administer entity extra field',
    ])
      ->setOptions($entity_options);
    $collection
      ->add("entity.{$entity_type_id}.extra_fields.add", $route);
    $route = new Route("{$entity_path}/extra-fields/{entity_extra_field}/edit");
    $route
      ->setDefaults([
      '_title' => 'Edit extra field',
      '_entity_form' => 'entity_extra_field.edit',
      'entity_type_id' => $entity_type_id,
    ])
      ->setRequirements([
      '_permission' => 'administer entity extra field',
    ])
      ->setOptions($entity_options);
    $collection
      ->add("entity.{$entity_type_id}.extra_fields.edit", $route);
    $route = new Route("{$entity_path}/extra-fields/{entity_extra_field}/delete");
    $route
      ->setDefaults([
      '_title' => 'Delete extra field',
      '_entity_form' => 'entity_extra_field.delete',
      'entity_type_id' => $entity_type_id,
    ])
      ->setRequirements([
      '_permission' => 'administer entity extra field',
    ])
      ->setOptions($entity_options);
    $collection
      ->add("entity.{$entity_type_id}.extra_fields.delete", $route);
  }
}