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
At the moment most of the time node route parameter is node object but in some cases it is entity id which is inconsistent and makes people write extra logic whenever they have to use that parameter.
To get entity id on node page:
$node = \Drupal::routeMatch()->getParameter('node');
$nid = $node->id();
and on node revision page:
$nid = \Drupal::routeMatch()->getParameter('node');
Proposed resolution
Decide whether Node route parameter should be entity id or node object and make all use cases implement the chosen method.
Remaining tasks
-
User interface changes
-
API changes
-
Comment | File | Size | Author |
---|---|---|---|
#10 | node_route_parameter_inconsistent-2495703-10.patch | 473 bytes | Michelle |
Comments
Comment #1
XanoCould you give an example of where this goes wrong?
Comment #2
lauriiiComment #3
lauriiiThis is bad because examples like this will break node revisions: http://drupal.stackexchange.com/questions/145823/how-to-get-current-node...
Comment #4
dawehnerGood observation.
We certainly want to have a full entity object available. The reason why the node object is not converted on the revision route is because, the controller looks like the following;
and $node is not used there. One way to fix that is by setting the following bit onto the route definition:
Comment #5
lauriiiComment #7
MichelleI made a patch based on dawehner's suggestion in #4 and it works to be able to use $node = \Drupal::routeMatch()->getParameter('node'); on both node view and node revision view without error.
Comment #8
lauriiiThanks for the patch! It would be nice to see test coverage for this
Comment #9
dawehnerWe should use
now. This loads even the right entity object.
Comment #10
MichelleI made the suggested change in #9 and tested it and it's still working. I don't know how to write tests so leaving this at Needs Work.
Comment #11
dawehnerGreat, well, to be honest I'm not sure what kind of test coverage @lauriii is asking for.
Comment #12
tstoecklerI don't understand. Isn't the {node} parameter of that route the node ID and should use the entity: upcasting, not entity_revision:?
Can someone explain?
Comment #13
dawehnerOh you are totally right, of course, so do you think we should just upcast to the node and be done with it?
Comment #14
tstoecklerYeah, I think #7 is the way to go.
Comment #15
nicrodgersComment #16
20th CreditAttribution: 20th commentedChanging status to duplicate of the above issue for the following reasons.