Updated: Comment #0
Problem/Motivation
menu_get_object() only works with menu_get_item() and the {menu_router} table.
#2089635: Convert non-test non-form page callbacks to routes and controllers moves most of the paths menu_get_object() is called on to routes, and it breaks.
It includes a generic hack.
Proposed resolution
menu_get_object() is used for two main reasons:
- To determine if it is on the "full" page of an entity, like node/%node, user/%user, taxonomy/term/%taxonomy_term
- To retrieve the object if its on the "full" page of an entity.
For case #1, it should check the route name, like if (\Drupal::request()->attributes->get(RouteObjectInterface::ROUTE_NAME) == 'node.view')
We can consider adding a helper function for that.
In the case of #2, more knowledge about the route is needed.
For example, for node/%node you would call $node = \Drupal::request()->attributes->get('node');
, but for
node/%node/revisions/%node_revision/view
you would need to do $vid = \Drupal::request()->attributes->get('node_revision'); $node = entity_revision_load('node', $vid);
(at least until we add a node_revision upcaster, if we so choose).
Remaining tasks
Replace all usage of menu_get_object() and remove it
User interface changes
N/A
API changes
menu_get_object() is removed
Possible new function like drupal_current_route_name() if desired
Comments
Comment #1
andypostThis needs replacement for
hook_help()
see related #2092641: node_help() does not allows to edit node translationsComment #2
ekes CreditAttribution: ekes commentedmenu_get_object()
is breaking (fatal error)taxonomy_term_is_page()
if the core taxonomy/term/% page view is enabled (views.view.taxonomy_term.yml
).To reproduce, add content in terms. Enable view. Clear cache. Then
$request->attributes->get(RouteObjectInterface::ROUTE_OBJECT)->getPath();
returns NULL. Hencetaxonomy_term_is_page()
tries to accessid
on non-object$page_term
.Comment #3
ekes CreditAttribution: ekes commentedSplit into three issues:-
#2095959: Remove instances of menu_get_object('node')
#2095961: Remove instances of menu_get_object('user')
#2095971: Remove instances of menu_get_object('taxonomy_term')
Comment #4
catchBlocks #2177031: Remove menu_get_item() and every use of it. , so bumping to critical.
Comment #5
xjmThat's a beta blocker, so this is too.
Comment #6
tim.plunkettComment #7
Berdir#2095959: Remove instances of menu_get_object('node') has been committed and removed the function, I'd say we deal with the change notice for the whole function there.
Comment #8
xjmBetter status then. Thanks!
Comment #9
xjm