Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When calling $entity->toUrl('add-page')
or $entity->toUrl('add-form')
, \Drupal\Core\Entity\Entity::uriRouteParameters()
incorrectly adds the entity ID as a route parameter. This is incorrect because adding an entity cannot possible require an entity ID, but it is not super problematic.
When calling $entity->toUrl('add-form')
for an entity type with bundles, Entity::uriRouteParameters()
fails to add the bundle to the route parameters which leads to an exception during URL generation.
Proposed resolution
In \Drupal\Core\Entity\Entity::uriRouteParameters()
:
- Do not add the entity ID as a route parameter for the
add-page
andadd-form
link templates. - Add the bundle for entity types with bundles for the
add-form
link template.
Remaining tasks
User interface changes
None.
API changes
None.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#10 | 2751835-10-entity-url-add.patch | 5.89 KB | tstoeckler |
Comments
Comment #2
tstoecklerComment #3
tstoecklerHere we go.
Extracted this from #2723579-16: [PP-1] NodeRouteProvider should extend DefaultHtmlRouteProvider but realized we don't need to check for a bundle entity type, a bundle key is completely sufficient.
Comment #4
tstoecklerUpdated patch from #2723579: [PP-1] NodeRouteProvider should extend DefaultHtmlRouteProvider. No interdiff because I literally copied the part of the patch from #2723579-21: [PP-1] NodeRouteProvider should extend DefaultHtmlRouteProvider. Stil postponed for test coverage, but could use a review nonetheless.
Comment #5
tstoecklerNow with test coverage, now that #2751395: Rewrite EntityUrlTest has been committed.
I added the
add-form
andadd-page
link relations to the language check inEntity::toUrl()
simply to avoid further code divergence. I.e. I would have to separate the tests forcollection
andadd-page
then (add-form
is already a special flower) instead of using a data provider. I can also revert that, if people don't like that. I added a comment to explain why I think we are doing that. I'm not really sure, though. I looked into #2428103: String formatter should link to its translation. where this code was introduced and the only comment is #2428103-18: String formatter should link to its translation.:...so yeah... :-)
Comment #8
tstoecklerComment #9
tstoecklerComment #10
tstoecklerThis needed a re-roll after #2751583: Entity::urlRouteParameters() calls getRevisionId() for non-revisionable entities.
Comment #11
dawehnerThat totally looks great for me!
Nice!
Comment #12
dawehner.
Comment #13
alexpottTest and logic looks great.
I thought about asking for the checks to be made into a method - but I think the current patch makes sense as the concerns are slightly different each time - the array just happnes to be the same.
Committed and pushed 53704b73f5f8add5e9a65d22f24fc500c1875fd9 to 8.2.x and 54e08c9 to 8.1.x. Thanks!