diff --git a/src/Controller/EntityCreateController.php b/src/Controller/EntityCreateController.php index 949a4dbeddfc482231363ff87d4e5fb04df77989..d9014fd5ce4f8143f68e8503a31c31c6e4be125e 100644 --- a/src/Controller/EntityCreateController.php +++ b/src/Controller/EntityCreateController.php @@ -119,7 +119,7 @@ class EntityCreateController extends ControllerBase { } /** - * Provides the add form for an entity of a specific bundle. + * Provides the add form for an entity. * * @param string $entity_type_id * The entity type ID. @@ -131,12 +131,13 @@ class EntityCreateController extends ControllerBase { */ public function addForm($entity_type_id, RouteMatchInterface $route_match) { $entity_type = $this->entityTypeManager()->getDefinition($entity_type_id); - $bundle_type = $entity_type->getBundleEntityType(); - $bundle_key = $entity_type->getKey('bundle'); - $bundle_name = $route_match->getRawParameter($bundle_type); - $entity = $this->entityTypeManager()->getStorage($entity_type_id)->create([ - $bundle_key => $bundle_name, - ]); + $values = []; + // Entities of this type have bundles, one was provided in the url. + if ($bundle_type = $entity_type->getBundleEntityType()) { + $bundle_key = $entity_type->getKey('bundle'); + $values[$bundle_key] = $route_match->getRawParameter($bundle_type); + } + $entity = $this->entityTypeManager()->getStorage($entity_type_id)->create($values); return $this->entityFormBuilder()->getForm($entity, 'add'); } @@ -154,14 +155,13 @@ class EntityCreateController extends ControllerBase { */ public function addFormTitle($entity_type_id, RouteMatchInterface $route_match) { $entity_type = $this->entityTypeManager()->getDefinition($entity_type_id); - $bundle_type = $entity_type->getBundleEntityType(); - $bundle_name = $route_match->getRawParameter($bundle_type); $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id); - if (count($bundles) == 1) { - $title = $this->t('Add @entity-type', ['@entity-type' => $entity_type->getLowercaseLabel()]); + if (count($bundles) > 1) { + $bundle_name = $route_match->getRawParameter($bundle_type); + $title = $this->t('Add @bundle', ['@bundle' => $bundles[$bundle_name]['label']]); } else { - $title = $this->t('Add @bundle', ['@bundle' => $bundles[$bundle_name]['label']]); + $title = $this->t('Add @entity-type', ['@entity-type' => $entity_type->getLowercaseLabel()]); } return $title; diff --git a/src/Routing/CreateHtmlRouteProvider.php b/src/Routing/CreateHtmlRouteProvider.php index bce607432af48df1126c363c504861a016aa2f35..e3be78a536b9cdcf71c1a1034191eb0e99ae74c5 100644 --- a/src/Routing/CreateHtmlRouteProvider.php +++ b/src/Routing/CreateHtmlRouteProvider.php @@ -41,6 +41,8 @@ class CreateHtmlRouteProvider implements EntityRouteProviderInterface { /** * Returns the add page route. * + * Built only for entity types that have bundle entity types. + * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * @@ -48,12 +50,13 @@ class CreateHtmlRouteProvider implements EntityRouteProviderInterface { * The generated route, if available. */ protected function addPageRoute(EntityTypeInterface $entity_type) { - if ($entity_type->hasLinkTemplate('add-page')) { + if ($entity_type->hasLinkTemplate('add-page') && $entity_type->getBundleEntityType()) { $route = new Route($entity_type->getLinkTemplate('add-page')); $route->setDefault('_controller', '\Drupal\entity\Controller\EntityCreateController::addPage'); $route->setDefault('_title_callback', '\Drupal\entity\Controller\EntityCreateController::addPageTitle'); $route->setDefault('entity_type_id', $entity_type->id()); $route->setRequirement('_entity_create_access', $entity_type->id()); + return $route; } } @@ -69,15 +72,18 @@ class CreateHtmlRouteProvider implements EntityRouteProviderInterface { */ protected function addFormRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('add-form')) { - $bundle_type = $entity_type->getBundleEntityType(); $route = new Route($entity_type->getLinkTemplate('add-form')); $route->setDefault('_controller', '\Drupal\entity\Controller\EntityCreateController::addForm'); $route->setDefault('_title_callback', '\Drupal\entity\Controller\EntityCreateController::addFormTitle'); $route->setDefault('entity_type_id', $entity_type->id()); - $route->setOption('parameters', [ - $bundle_type => ['type' => 'entity:' . $bundle_type], - ]); $route->setRequirement('_entity_create_access', $entity_type->id()); + // The route needs a bundle parameter. + if ($bundle_type = $entity_type->getBundleEntityType()) { + $route->setOption('parameters', [ + $bundle_type => ['type' => 'entity:' . $bundle_type], + ]); + } + return $route; } }