3 namespace Drupal\Core\Entity\Routing;
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;
17 * Provides HTML routes for entities.
19 * This class provides the following routes for entities, with title and access
27 * - delete-multiple-form
29 * @see \Drupal\Core\Entity\Routing\AdminHtmlRouteProvider.
31 class DefaultHtmlRouteProvider implements EntityRouteProviderInterface, EntityHandlerInterface {
34 * The entity type manager.
36 * @var \Drupal\Core\Entity\EntityManagerInterface
38 protected $entityTypeManager;
41 * The entity field manager.
43 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
45 protected $entityFieldManager;
48 * Constructs a new DefaultHtmlRouteProvider.
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.
55 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
56 $this->entityTypeManager = $entity_type_manager;
57 $this->entityFieldManager = $entity_field_manager;
63 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
65 $container->get('entity_type.manager'),
66 $container->get('entity_field.manager')
73 public function getRoutes(EntityTypeInterface $entity_type) {
74 $collection = new RouteCollection();
76 $entity_type_id = $entity_type->id();
78 if ($add_page_route = $this->getAddPageRoute($entity_type)) {
79 $collection->add("entity.{$entity_type_id}.add_page", $add_page_route);
82 if ($add_form_route = $this->getAddFormRoute($entity_type)) {
83 $collection->add("entity.{$entity_type_id}.add_form", $add_form_route);
86 if ($canonical_route = $this->getCanonicalRoute($entity_type)) {
87 $collection->add("entity.{$entity_type_id}.canonical", $canonical_route);
90 if ($edit_route = $this->getEditFormRoute($entity_type)) {
91 $collection->add("entity.{$entity_type_id}.edit_form", $edit_route);
94 if ($delete_route = $this->getDeleteFormRoute($entity_type)) {
95 $collection->add("entity.{$entity_type_id}.delete_form", $delete_route);
98 if ($collection_route = $this->getCollectionRoute($entity_type)) {
99 $collection->add("entity.{$entity_type_id}.collection", $collection_route);
102 if ($delete_multiple_route = $this->getDeleteMultipleFormRoute($entity_type)) {
103 $collection->add('entity.' . $entity_type->id() . '.delete_multiple_form', $delete_multiple_route);
110 * Gets the add page route.
112 * Built only for entity types that have bundles.
114 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
117 * @return \Symfony\Component\Routing\Route|null
118 * The generated route, if available.
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());
133 * Gets the add-form route.
135 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
138 * @return \Symfony\Component\Routing\Route|null
139 * The generated route, if available.
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')) {
150 $route->setDefaults([
151 '_entity_form' => "{$entity_type_id}.{$operation}",
152 'entity_type_id' => $entity_type_id,
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);
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 . '}');
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+');
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;
188 $route->setOption('parameters', [$bundle_entity_type_id => $bundle_entity_parameter]);
191 // If the bundles are not entities, the bundle key is used as the
192 // route parameter name directly.
194 ->setDefault('bundle_parameter', $bundle_key)
195 ->setRequirement('_entity_create_access', $entity_type_id . ':{' . $bundle_key . '}');
200 ->setDefault('_title_callback', EntityController::class . '::addTitle')
201 ->setRequirement('_entity_create_access', $entity_type_id);
209 * Gets the canonical route.
211 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
214 * @return \Symfony\Component\Routing\Route|null
215 * The generated route, if available.
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'));
223 '_entity_view' => "{$entity_type_id}.full",
224 '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
226 ->setRequirement('_entity_access', "{$entity_type_id}.view")
227 ->setOption('parameters', [
228 $entity_type_id => ['type' => 'entity:' . $entity_type_id],
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+');
241 * Gets the edit-form route.
243 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
246 * @return \Symfony\Component\Routing\Route|null
247 * The generated route, if available.
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')) {
260 '_entity_form' => "{$entity_type_id}.{$operation}",
261 '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::editTitle',
263 ->setRequirement('_entity_access', "{$entity_type_id}.update")
264 ->setOption('parameters', [
265 $entity_type_id => ['type' => 'entity:' . $entity_type_id],
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+');
278 * Gets the delete-form route.
280 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
283 * @return \Symfony\Component\Routing\Route|null
284 * The generated route, if available.
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'));
292 '_entity_form' => "{$entity_type_id}.delete",
293 '_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::deleteTitle',
295 ->setRequirement('_entity_access', "{$entity_type_id}.delete")
296 ->setOption('parameters', [
297 $entity_type_id => ['type' => 'entity:' . $entity_type_id],
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+');
310 * Gets the collection route.
312 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
315 * @return \Symfony\Component\Routing\Route|null
316 * The generated route, if available.
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();
325 $route = new Route($entity_type->getLinkTemplate('collection'));
328 '_entity_list' => $entity_type->id(),
329 '_title' => $label->getUntranslatedString(),
330 '_title_arguments' => $label->getArguments(),
331 '_title_context' => $label->getOption('context'),
333 ->setRequirement('_permission', $admin_permission);
340 * Gets the type of the ID key for a given entity type.
342 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
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.
349 protected function getEntityTypeIdKeyType(EntityTypeInterface $entity_type) {
350 if (!$entity_type->entityClassImplements(FieldableEntityInterface::class)) {
354 $field_storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type->id());
355 return $field_storage_definitions[$entity_type->getKey('id')]->getType();
359 * Returns the delete multiple form route.
361 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
364 * @return \Symfony\Component\Routing\Route|null
365 * The generated route, if available.
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());