RouteSubscriber.php in Web Service Data 2.0.x
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;
class RouteSubscriber extends RouteSubscriberBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
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')) {
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,
];
}
$options['_field_ui'] = TRUE;
$defaults = [
'entity_type_id' => $entity_type_id,
];
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);
}
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = [
'onAlterRoutes',
-100,
];
return $events;
}
}