You are here

RouteSubscriber.php in Web Service Data 2.0.x

Same filename and directory in other branches
  1. 8 modules/wsdata_field/src/Routing/RouteSubscriber.php

File

modules/wsdata_field/src/Routing/RouteSubscriber.php
View source
<?php

namespace Drupal\wsdata_field\Routing;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * Subscriber for Field UI routes.
 */
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;
  }

}

Classes

Namesort descending Description
RouteSubscriber Subscriber for Field UI routes.