1415a1aba66ad13459a94f28d32a4930a5a0eb24
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Routing / DefaultHtmlRouteProvider.php
1 <?php
2
3 namespace Drupal\Core\Entity\Routing;
4
5 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
6 use Drupal\Core\Entity\Controller\EntityController;
7 use Drupal\Core\Entity\EntityFieldManagerInterface;
8 use Drupal\Core\Entity\EntityHandlerInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Entity\FieldableEntityInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\Routing\Route;
14 use Symfony\Component\Routing\RouteCollection;
15
16 /**
17  * Provides HTML routes for entities.
18  *
19  * This class provides the following routes for entities, with title and access
20  * callbacks:
21  * - canonical
22  * - add-page
23  * - add-form
24  * - edit-form
25  * - delete-form
26  * - collection
27  * - delete-multiple-form
28  *
29  * @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider.
30  */
31 class DefaultHtmlRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
32
33   /**
34    * The entity type manager.
35    *
36    * @var \Drupal\Core\Entity\EntityManagerInterface
37    */
38   protected $entityTypeManager;
39
40   /**
41    * The entity field manager.
42    *
43    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
44    */
45   protected $entityFieldManager;
46
47   /**
48    * Constructs a new DefaultHtmlRouteProvider.
49    *
50    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
51    *   The entity type manager.
52    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
53    *   The entity field manager.
54    */
55   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
56     $this->entityTypeManager = $entity_type_manager;
57     $this->entityFieldManager = $entity_field_manager;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
64     return new static(
65       $container->get('entity_type.manager'),
66       $container->get('entity_field.manager')
67     );
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getRoutes(EntityTypeInterface $entity_type) {
74     $collection = new RouteCollection();
75
76     $entity_type_id = $entity_type->id();
77
78     if ($add_page_route = $this->getAddPageRoute($entity_type)) {
79       $collection->add("entity.{$entity_type_id}.add_page", $add_page_route);
80     }
81
82     if ($add_form_route = $this->getAddFormRoute($entity_type)) {
83       $collection->add("entity.{$entity_type_id}.add_form", $add_form_route);
84     }
85
86     if ($canonical_route = $this->getCanonicalRoute($entity_type)) {
87       $collection->add("entity.{$entity_type_id}.canonical", $canonical_route);
88     }
89
90     if ($edit_route = $this->getEditFormRoute($entity_type)) {
91       $collection->add("entity.{$entity_type_id}.edit_form", $edit_route);
92     }
93
94     if ($delete_route = $this->getDeleteFormRoute($entity_type)) {
95       $collection->add("entity.{$entity_type_id}.delete_form", $delete_route);
96     }
97
98     if ($collection_route = $this->getCollectionRoute($entity_type)) {
99       $collection->add("entity.{$entity_type_id}.collection", $collection_route);
100     }
101
102     if ($delete_multiple_route = $this->getDeleteMultipleFormRoute($entity_type)) {
103       $collection->add('entity.' . $entity_type->id() . '.delete_multiple_form', $delete_multiple_route);
104     }
105
106     return $collection;
107   }
108
109   /**
110    * Gets the add page route.
111    *
112    * Built only for entity types that have bundles.
113    *
114    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
115    *   The entity type.
116    *
117    * @return \Symfony\Component\Routing\Route|null
118    *   The generated route, if available.
119    */
120   protected function getAddPageRoute(EntityTypeInterface $entity_type) {
121     if ($entity_type->hasLinkTemplate('add-page') && $entity_type->getKey('bundle')) {
122       $route = new Route($entity_type->getLinkTemplate('add-page'));
123       $route->setDefault('_controller', EntityController::class . '::addPage');
124       $route->setDefault('_title_callback', EntityController::class . '::addTitle');
125       $route->setDefault('entity_type_id', $entity_type->id());
126       $route->setRequirement('_entity_create_any_access', $entity_type->id());
127
128       return $route;
129     }
130   }
131
132   /**
133    * Gets the add-form route.
134    *
135    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
136    *   The entity type.
137    *
138    * @return \Symfony\Component\Routing\Route|null
139    *   The generated route, if available.
140    */
141   protected function getAddFormRoute(EntityTypeInterface $entity_type) {
142     if ($entity_type->hasLinkTemplate('add-form')) {
143       $entity_type_id = $entity_type->id();
144       $route = new Route($entity_type->getLinkTemplate('add-form'));
145       // Use the add form handler, if available, otherwise default.
146       $operation = 'default';
147       if ($entity_type->getFormClass('add')) {
148         $operation = 'add';
149       }
150       $route->setDefaults([
151         '_entity_form' => "{$entity_type_id}.{$operation}",
152         'entity_type_id' => $entity_type_id,
153       ]);
154
155       // If the entity has bundles, we can provide a bundle-specific title
156       // and access requirements.
157       $expected_parameter = $entity_type->getBundleEntityType() ?: $entity_type->getKey('bundle');
158       // @todo: We have to check if a route contains a bundle in its path as
159       //   test entities have inconsistent usage of "add-form" link templates.
160       //   Fix it in https://www.drupal.org/node/2699959.
161       if (($bundle_key = $entity_type->getKey('bundle')) && strpos($route->getPath(), '{' . $expected_parameter . '}') !== FALSE) {
162         $route->setDefault('_title_callback', EntityController::class . '::addBundleTitle');
163         // If the bundles are entities themselves, we can add parameter
164         // information to the route options.
165         if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
166           $bundle_entity_type = $this->entityTypeManager->getDefinition($bundle_entity_type_id);
167
168           $route
169             // The title callback uses the value of the bundle parameter to
170             // fetch the respective bundle at runtime.
171             ->setDefault('bundle_parameter', $bundle_entity_type_id)
172             ->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_entity_type_id . '}');
173
174           // Entity types with serial IDs can specify this in their route
175           // requirements, improving the matching process.
176           if ($this->getEntityTypeIdKeyType($bundle_entity_type) === 'integer') {
177             $route->setRequirement($entity_type_id, '\d+');
178           }
179
180           $bundle_entity_parameter = ['type' => 'entity:' . $bundle_entity_type_id];
181           if ($bundle_entity_type instanceof ConfigEntityTypeInterface) {
182             // The add page might be displayed on an admin path. Even then, we
183             // need to load configuration overrides so that, for example, the
184             // bundle label gets translated correctly.
185             // @see \Drupal\Core\ParamConverter\AdminPathConfigEntityConverter
186             $bundle_entity_parameter['with_config_overrides'] = TRUE;
187           }
188           $route->setOption('parameters', [$bundle_entity_type_id => $bundle_entity_parameter]);
189         }
190         else {
191           // If the bundles are not entities, the bundle key is used as the
192           // route parameter name directly.
193           $route
194             ->setDefault('bundle_parameter', $bundle_key)
195             ->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_key . '}');
196         }
197       }
198       else {
199         $route
200           ->setDefault('_title_callback', EntityController::class . '::addTitle')
201           ->setRequirement('_entity_create_access', $entity_type_id);
202       }
203
204       return $route;
205     }
206   }
207
208   /**
209    * Gets the canonical route.
210    *
211    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
212    *   The entity type.
213    *
214    * @return \Symfony\Component\Routing\Route|null
215    *   The generated route, if available.
216    */
217   protected function getCanonicalRoute(EntityTypeInterface $entity_type) {
218     if ($entity_type->hasLinkTemplate('canonical') && $entity_type->hasViewBuilderClass()) {
219       $entity_type_id = $entity_type->id();
220       $route = new Route($entity_type->getLinkTemplate('canonical'));
221       $route
222         ->addDefaults([
223           '_entity_view' => "{$entity_type_id}.full",
224           '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
225         ])
226         ->setRequirement('_entity_access', "{$entity_type_id}.view")
227         ->setOption('parameters', [
228           $entity_type_id => ['type' => 'entity:' . $entity_type_id],
229         ]);
230
231       // Entity types with serial IDs can specify this in their route
232       // requirements, improving the matching process.
233       if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
234         $route->setRequirement($entity_type_id, '\d+');
235       }
236       return $route;
237     }
238   }
239
240   /**
241    * Gets the edit-form route.
242    *
243    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
244    *   The entity type.
245    *
246    * @return \Symfony\Component\Routing\Route|null
247    *   The generated route, if available.
248    */
249   protected function getEditFormRoute(EntityTypeInterface $entity_type) {
250     if ($entity_type->hasLinkTemplate('edit-form')) {
251       $entity_type_id = $entity_type->id();
252       $route = new Route($entity_type->getLinkTemplate('edit-form'));
253       // Use the edit form handler, if available, otherwise default.
254       $operation = 'default';
255       if ($entity_type->getFormClass('edit')) {
256         $operation = 'edit';
257       }
258       $route
259         ->setDefaults([
260           '_entity_form' => "{$entity_type_id}.{$operation}",
261           '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::editTitle',
262         ])
263         ->setRequirement('_entity_access', "{$entity_type_id}.update")
264         ->setOption('parameters', [
265           $entity_type_id => ['type' => 'entity:' . $entity_type_id],
266         ]);
267
268       // Entity types with serial IDs can specify this in their route
269       // requirements, improving the matching process.
270       if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
271         $route->setRequirement($entity_type_id, '\d+');
272       }
273       return $route;
274     }
275   }
276
277   /**
278    * Gets the delete-form route.
279    *
280    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
281    *   The entity type.
282    *
283    * @return \Symfony\Component\Routing\Route|null
284    *   The generated route, if available.
285    */
286   protected function getDeleteFormRoute(EntityTypeInterface $entity_type) {
287     if ($entity_type->hasLinkTemplate('delete-form')) {
288       $entity_type_id = $entity_type->id();
289       $route = new Route($entity_type->getLinkTemplate('delete-form'));
290       $route
291         ->addDefaults([
292           '_entity_form' => "{$entity_type_id}.delete",
293           '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::deleteTitle',
294         ])
295         ->setRequirement('_entity_access', "{$entity_type_id}.delete")
296         ->setOption('parameters', [
297           $entity_type_id => ['type' => 'entity:' . $entity_type_id],
298         ]);
299
300       // Entity types with serial IDs can specify this in their route
301       // requirements, improving the matching process.
302       if ($this->getEntityTypeIdKeyType($entity_type) === 'integer') {
303         $route->setRequirement($entity_type_id, '\d+');
304       }
305       return $route;
306     }
307   }
308
309   /**
310    * Gets the collection route.
311    *
312    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
313    *   The entity type.
314    *
315    * @return \Symfony\Component\Routing\Route|null
316    *   The generated route, if available.
317    */
318   protected function getCollectionRoute(EntityTypeInterface $entity_type) {
319     // If the entity type does not provide an admin permission, there is no way
320     // to control access, so we cannot provide a route in a sensible way.
321     if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass() && ($admin_permission = $entity_type->getAdminPermission())) {
322       /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
323       $label = $entity_type->getCollectionLabel();
324
325       $route = new Route($entity_type->getLinkTemplate('collection'));
326       $route
327         ->addDefaults([
328           '_entity_list' => $entity_type->id(),
329           '_title' => $label->getUntranslatedString(),
330           '_title_arguments' => $label->getArguments(),
331           '_title_context' => $label->getOption('context'),
332         ])
333         ->setRequirement('_permission', $admin_permission);
334
335       return $route;
336     }
337   }
338
339   /**
340    * Gets the type of the ID key for a given entity type.
341    *
342    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
343    *   An entity type.
344    *
345    * @return string|null
346    *   The type of the ID key for a given entity type, or NULL if the entity
347    *   type does not support fields.
348    */
349   protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
350     if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
351       return NULL;
352     }
353
354     $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
355     return $field_storage_definitions[$entity_type->getKey('id')]->getType();
356   }
357
358   /**
359    * Returns the delete multiple form route.
360    *
361    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
362    *   The entity type.
363    *
364    * @return \Symfony\Component\Routing\Route|null
365    *   The generated route, if available.
366    */
367   protected function getDeleteMultipleFormRoute(EntityTypeInterface $entity_type) {
368     if ($entity_type->hasLinkTemplate('delete-multiple-form') && $entity_type->hasHandlerClass('form', 'delete-multiple-confirm')) {
369       $route = new Route($entity_type->getLinkTemplate('delete-multiple-form'));
370       $route->setDefault('_form', $entity_type->getFormClass('delete-multiple-confirm'));
371       $route->setDefault('entity_type_id', $entity_type->id());
372       $route->setRequirement('_entity_delete_multiple_access', $entity_type->id());
373       return $route;
374     }
375   }
376
377 }