comparison core/modules/field_ui/src/Routing/RouteSubscriber.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\field_ui\Routing;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Routing\RouteSubscriberBase;
7 use Drupal\Core\Routing\RoutingEvents;
8 use Symfony\Component\Routing\Route;
9 use Symfony\Component\Routing\RouteCollection;
10
11 /**
12 * Subscriber for Field UI routes.
13 */
14 class RouteSubscriber extends RouteSubscriberBase {
15
16 /**
17 * The entity type manager
18 *
19 * @var \Drupal\Core\Entity\EntityManagerInterface
20 */
21 protected $manager;
22
23 /**
24 * Constructs a RouteSubscriber object.
25 *
26 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
27 * The entity type manager.
28 */
29 public function __construct(EntityManagerInterface $manager) {
30 $this->manager = $manager;
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 protected function alterRoutes(RouteCollection $collection) {
37 foreach ($this->manager->getDefinitions() as $entity_type_id => $entity_type) {
38 if ($route_name = $entity_type->get('field_ui_base_route')) {
39 // Try to get the route from the current collection.
40 if (!$entity_route = $collection->get($route_name)) {
41 continue;
42 }
43 $path = $entity_route->getPath();
44
45 $options = $entity_route->getOptions();
46 if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
47 $options['parameters'][$bundle_entity_type] = [
48 'type' => 'entity:' . $bundle_entity_type,
49 ];
50 }
51 // Special parameter used to easily recognize all Field UI routes.
52 $options['_field_ui'] = TRUE;
53
54 $defaults = [
55 'entity_type_id' => $entity_type_id,
56 ];
57 // If the entity type has no bundles and it doesn't use {bundle} in its
58 // admin path, use the entity type.
59 if (strpos($path, '{bundle}') === FALSE) {
60 $defaults['bundle'] = !$entity_type->hasKey('bundle') ? $entity_type_id : '';
61 }
62
63 $route = new Route(
64 "$path/fields/{field_config}",
65 [
66 '_entity_form' => 'field_config.edit',
67 '_title_callback' => '\Drupal\field_ui\Form\FieldConfigEditForm::getTitle',
68 ] + $defaults,
69 ['_entity_access' => 'field_config.update'],
70 $options
71 );
72 $collection->add("entity.field_config.{$entity_type_id}_field_edit_form", $route);
73
74 $route = new Route(
75 "$path/fields/{field_config}/storage",
76 ['_entity_form' => 'field_storage_config.edit'] + $defaults,
77 ['_permission' => 'administer ' . $entity_type_id . ' fields'],
78 $options
79 );
80 $collection->add("entity.field_config.{$entity_type_id}_storage_edit_form", $route);
81
82 $route = new Route(
83 "$path/fields/{field_config}/delete",
84 ['_entity_form' => 'field_config.delete'] + $defaults,
85 ['_entity_access' => 'field_config.delete'],
86 $options
87 );
88 $collection->add("entity.field_config.{$entity_type_id}_field_delete_form", $route);
89
90 $route = new Route(
91 "$path/fields",
92 [
93 '_controller' => '\Drupal\field_ui\Controller\FieldConfigListController::listing',
94 '_title' => 'Manage fields',
95 ] + $defaults,
96 ['_permission' => 'administer ' . $entity_type_id . ' fields'],
97 $options
98 );
99 $collection->add("entity.{$entity_type_id}.field_ui_fields", $route);
100
101 $route = new Route(
102 "$path/fields/add-field",
103 [
104 '_form' => '\Drupal\field_ui\Form\FieldStorageAddForm',
105 '_title' => 'Add field',
106 ] + $defaults,
107 ['_permission' => 'administer ' . $entity_type_id . ' fields'],
108 $options
109 );
110 $collection->add("field_ui.field_storage_config_add_$entity_type_id", $route);
111
112 $route = new Route(
113 "$path/form-display",
114 [
115 '_entity_form' => 'entity_form_display.edit',
116 '_title' => 'Manage form display',
117 'form_mode_name' => 'default',
118 ] + $defaults,
119 ['_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display'],
120 $options
121 );
122 $collection->add("entity.entity_form_display.{$entity_type_id}.default", $route);
123
124 $route = new Route(
125 "$path/form-display/{form_mode_name}",
126 [
127 '_entity_form' => 'entity_form_display.edit',
128 '_title' => 'Manage form display',
129 ] + $defaults,
130 ['_field_ui_form_mode_access' => 'administer ' . $entity_type_id . ' form display'],
131 $options
132 );
133 $collection->add("entity.entity_form_display.{$entity_type_id}.form_mode", $route);
134
135 $route = new Route(
136 "$path/display",
137 [
138 '_entity_form' => 'entity_view_display.edit',
139 '_title' => 'Manage display',
140 'view_mode_name' => 'default',
141 ] + $defaults,
142 ['_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'],
143 $options
144 );
145 $collection->add("entity.entity_view_display.{$entity_type_id}.default", $route);
146
147 $route = new Route(
148 "$path/display/{view_mode_name}",
149 [
150 '_entity_form' => 'entity_view_display.edit',
151 '_title' => 'Manage display',
152 ] + $defaults,
153 ['_field_ui_view_mode_access' => 'administer ' . $entity_type_id . ' display'],
154 $options
155 );
156 $collection->add("entity.entity_view_display.{$entity_type_id}.view_mode", $route);
157 }
158 }
159 }
160
161 /**
162 * {@inheritdoc}
163 */
164 public static function getSubscribedEvents() {
165 $events = parent::getSubscribedEvents();
166 $events[RoutingEvents::ALTER] = ['onAlterRoutes', -100];
167 return $events;
168 }
169
170 }