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;
  }

Issue fork drupal-2921093

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

marcusx created an issue. See original summary.

marcusx’s picture

Issue summary: View changes
marcusx’s picture

Version: 8.3.x-dev » 8.5.x-dev
tstoeckler’s picture

Yes, this is a legitimate bug report. I hit this before in #2723615: Use entity route providers for all content entity types. In patch #18 over there I added a workaround to MenuLinkContentForm. However, solving this in a generic way makes more sense, so it's great you opened this issue.

Can you provide a patch which simply changes the check in the if-clause from getRawParameter() to getParameter()? Then we can see if anything breaks.

rosk0’s picture

Status: Active » Needs review
StatusFileSize
new645 bytes

Lets try the concept but it doesn't seem like a correct approach.

tstoeckler’s picture

Hmm... I thought this issue was about the following call:

elseif ($route_match->getRawParameter($bundle_key)) {

not the one where the entity type ID is fetched?

rosk0’s picture

I obviously done a mistake.
Also obvious that we need to write test first.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mglaman’s picture

StatusFileSize
new697 bytes

Ran into this as well. Instead of adjusting existing code, a new elseif is added to support default values if there are no raw parameters. Just to prevent and unknown repercussions.

Status: Needs review » Needs work

The last submitted patch, 9: 2921093-9.patch, failed testing. View results

tstoeckler’s picture

Hmm.. raw parameters are always a subset of the parameters, so I still think changing getRawParameter() to getParameter() makes sense.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

watergate’s picture

Having the same problem and not understanding why explicitly using getRawParameter() in the checks and then using the value from the getParameter() call. Can't/shouldn't we simply use getParameter() in the checks as well?

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

ohorbatiuk’s picture

StatusFileSize
new716 bytes

#9 with fixed a path to modified file.

anmolgoyal74’s picture

Status: Needs work » Needs review
StatusFileSize
new839 bytes

Patch for 9.2.x in a shorter way.
not able to generate the interdiff.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mrshowerman’s picture

StatusFileSize
new1.11 KB

I agree with @tstoeckler.

If an entity type has been provided as a URL parameter, it will be returned by both getRawParameter() (as a string) and getParameter() (as an object). Thus, checking the raw parameter and returning the processed parameter works in this case.

But if the parameter has just been set in the routing definition, it will only be present in getParameter().

Adding a patch which simply replaces 2 calls to getRawParameter() with getParameter().

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

sjpeters79’s picture

I ran into this issue recently while trying to create routes for node add and edit pages. For the longest time I couldn't figure out why my route was not working until I added node_type as parameter in my route path.

This patch seems like an appropriate fix. I've tested the latest patch and it works for me.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mxh’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

For BC we should keep the call to ->getRawParameter(), and if that method doesn't return anything, "fallback" to ->getParameter(). Then it should be checked whether ->getParameter() either returns a scalar (string or int) or already returns an entity (since this method can provide processed / converted values).

That change also needs test coverage.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

catch’s picture

Issue tags: +Needs reroll

I'm not sure I understand #26.

The current patch looks like this:

+++ b/core/lib/Drupal/Core/Entity/EntityForm.php
@@ -359,10 +359,10 @@ public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entit
-        if (($bundle_entity_type_id = $entity_type->getBundleEntityType()) && $route_match->getRawParameter($bundle_entity_type_id)) {
+        if (($bundle_entity_type_id = $entity_type->getBundleEntityType()) && $route_match->getParameter($bundle_entity_type_id)) {
           $values[$bundle_key] = $route_match->getParameter($bundle_entity_type_id)->id();
         }

Note the assignment doesn't change - that is calling ::getParameter(), it's only the if check that calls getRawParameter().

Tagging for re-roll - this needs conversion to an MR. We can probably base the test coverage on the issues found in #2723615: Use entity route providers for all content entity types or I wonder if that issue is close enough, we could rely on that issue for the test coverage and just go ahead here (once it's demonstrated that it fixes the problems over there).

mrshowerman’s picture

Issue tags: -Needs reroll

Created MR based on #21. Leaving as NW for the decision about test coverage.

mxh’s picture

I'm not sure I understand #26.

My comment #26 is too long ago to provide more context at this time. Guess I made that advise up to minimize potential compatibility breaks. Definitively it was not made up because of any variable assignments. It seems both methods are detached from each other on runtime, which is probably not reproducable by static code checks but will pop up once recognized on runtime.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.