You are here

protected function RouteSubscriber::alterRoutes in CiviCRM Entity 8.3

Alters existing routes for a specific collection.

Parameters

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

Overrides RouteSubscriberBase::alterRoutes

File

src/Routing/RouteSubscriber.php, line 46

Class

RouteSubscriber
Alters generated routes for CiviCRM Entity entity definitions.

Namespace

Drupal\civicrm_entity\Routing

Code

protected function alterRoutes(RouteCollection $collection) {

  // Only run if Field UI is installed.
  if (!$this->moduleHandler
    ->moduleExists('field_ui')) {
    return;
  }
  $has_layout_builder = $this->moduleHandler
    ->moduleExists('layout_builder');
  foreach ($this->entityTypeManager
    ->getDefinitions() as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->get('civicrm_entity_ui_exposed')) {
      continue;
    }
    if (!$entity_type
      ->hasKey('bundle')) {
      continue;
    }
    $field_ui_routes = [
      "entity.{$entity_type_id}.field_ui_fields" => [
        'bundle' => $entity_type_id,
      ],
      "entity.field_config.{$entity_type_id}_field_edit_form" => [
        'bundle' => $entity_type_id,
      ],
      "entity.field_config.{$entity_type_id}_storage_edit_form" => [
        'bundle' => $entity_type_id,
      ],
      "entity.field_config.{$entity_type_id}_field_delete_form" => [
        'bundle' => $entity_type_id,
      ],
      "field_ui.field_storage_config_add_{$entity_type_id}" => [
        'bundle' => $entity_type_id,
      ],
      "entity.entity_form_display.{$entity_type_id}.default" => [
        'bundle' => $entity_type_id,
      ],
      "entity.entity_form_display.{$entity_type_id}.form_mode" => [
        'bundle' => $entity_type_id,
      ],
      "entity.entity_view_display.{$entity_type_id}.default" => [
        'bundle' => $entity_type_id,
      ],
      "entity.entity_view_display.{$entity_type_id}.view_mode" => [
        'bundle' => $entity_type_id,
      ],
    ];
    if ($has_layout_builder) {

      // @todo we should iterate over the section storage definitions.
      //   that means we'd need to conditionally inject the manage service.
      // @see \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage::buildRoutes
      $field_ui_routes["layout_builder.defaults.{$entity_type_id}.view"] = [
        'bundle' => $entity_type_id,
      ];

      // @see \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage::buildRoutes
      $field_ui_routes["layout_builder.overrides.{$entity_type_id}.view"] = [
        'bundle' => $entity_type_id,
      ];
      $field_ui_routes["layout_builder.defaults.{$entity_type_id}.discard_changes"] = [
        'bundle' => $entity_type_id,
      ];
      $field_ui_routes["layout_builder.defaults.{$entity_type_id}.disable"] = [
        'bundle' => $entity_type_id,
      ];
    }
    foreach ($field_ui_routes as $route_name => $defaults) {
      $route = $collection
        ->get($route_name);
      assert($route !== NULL);
      foreach ($defaults as $name => $default) {
        $route
          ->setDefault($name, $default);
      }
    }
  }
}