View source
<?php
namespace Drupal\field_tools\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] = array(
'type' => 'entity:' . $bundle_entity_type,
);
}
$options['_field_ui'] = TRUE;
$defaults = array(
'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/{field_config}/clone", array(
'_entity_form' => 'field_config.clone',
'_title' => 'Clone field',
) + $defaults, array(
'_entity_access' => 'field_config.update',
), $options);
$collection
->add("entity.field_config.{$entity_type_id}_field_tools_clone_form", $route);
$route = new Route("{$path}/fields/tools/clone-fields", array(
'_form' => '\\Drupal\\field_tools\\Form\\FieldBulkCloneForm',
'_title' => 'Clone fields',
) + $defaults, array(
'_permission' => 'administer ' . $entity_type_id . ' fields',
), $options);
$collection
->add("field_tools.field_bulk_clone_{$entity_type_id}", $route);
$route = new Route("{$path}/fields/tools/clone-displays", array(
'_form' => '\\Drupal\\field_tools\\Form\\EntityDisplayBulkCloneForm',
'_title' => 'Clone displays',
) + $defaults, array(
'_permission' => 'administer ' . $entity_type_id . ' fields',
), $options);
$collection
->add("field_tools.displays_clone_{$entity_type_id}", $route);
$route = new Route("{$path}/fields/tools/copy-display-settings", array(
'_form' => '\\Drupal\\field_tools\\Form\\EntityDisplaySettingsBulkCopyForm',
'_title' => 'Copy display settings',
) + $defaults, array(
'_permission' => 'administer ' . $entity_type_id . ' fields',
), $options);
$collection
->add("field_tools.displays_settings_copy_{$entity_type_id}", $route);
$route = new Route("{$path}/fields/tools/export-to-code", array(
'_form' => '\\Drupal\\field_tools\\Form\\ConfigFieldsExportToCodeForm',
'_title' => 'Export to base fields code',
) + $defaults, array(
'_permission' => 'administer ' . $entity_type_id . ' fields',
), $options);
$collection
->add("field_tools.export_to_code_{$entity_type_id}", $route);
}
}
}
public static function getSubscribedEvents() {
$events = parent::getSubscribedEvents();
$events[RoutingEvents::ALTER] = array(
'onAlterRoutes',
-100,
);
return $events;
}
}