Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295826

Providing default route value for entity forms is not possible

$
0
0

If you try to create a static route for adding an entity you cannot provide the entity type as a default.

e.g.:
The following route is not working. You get an:

Drupal\Core\Entity\EntityStorageException: Missing bundle for entity type example in Drupal\Core\Entity\ContentEntityStorageBase->doCreate() (line 83 of core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php).
entity.example.add_onboarding_form:
  path: '/example/onboarding'
  defaults:
    _entity_form: example.onboarding_add
    _title: 'Create an onboarding example Entity'
    example_type: 'my_bundle'
  requirements:
    _permission: 'edit example entities'
  options:
    parameters:
      example_type:
        type: entity:example_type

But this is working
e.g.:

entity.example.add_onboarding_form:
  path: '/example/onboarding/{example_type}'
  defaults:
    _entity_form: example.onboarding_add
    _title: 'Create an onboarding example Entity'
    example_type: 'my_bundle'
  requirements:
    _permission: 'edit example entities'
  options:
    parameters:
      example_type:
        type: entity:example_type

Reason is in \Drupal\Core\Entity\EntityForm::getEntityFromRouteMatch

There is this line. Where we check getRawParameter. If the placeholder is not in the route definition this is empty. The processed parameter has the correct information. And if you remove this check the right example_type gets picked up in the next line.

if (($bundle_entity_type_id = $entity_type->getBundleEntityType()) && $route_match->getRawParameter($bundle_entity_type_id)) {

What is the reason for checking the Raw Parameter here? I have no idea about the intend so its hard to say if this is a bug and if and how it can be fixed without breaking other things. Maybe somebody can enlight me.

Complete method for more context:

  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
    if ($route_match->getRawParameter($entity_type_id) !== NULL) {
      $entity = $route_match->getParameter($entity_type_id);
    }
    else {
      $values = [];
      // If the entity has bundles, fetch it from the route match.
      $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
      if ($bundle_key = $entity_type->getKey('bundle')) {
        if (($bundle_entity_type_id = $entity_type->getBundleEntityType()) && $route_match->getRawParameter($bundle_entity_type_id)) {
         $match = $route_match->getParameter($bundle_entity_type_id);
          $values[$bundle_key] = $route_match->getParameter($bundle_entity_type_id)->id();
        }
        elseif ($route_match->getRawParameter($bundle_key)) {
          $values[$bundle_key] = $route_match->getParameter($bundle_key);
        }
      }

      $entity = $this->entityTypeManager->getStorage($entity_type_id)->create($values);
    }

    return $entity;
  }

Viewing all articles
Browse latest Browse all 295826

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>