Updated: Comment #0


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:

  1. To determine if it is on the "full" page of an entity, like node/%node, user/%user, taxonomy/term/%taxonomy_term
  2. 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


API changes

menu_get_object() is removed
Possible new function like drupal_current_route_name() if desired


andypost’s picture

This needs replacement for hook_help() see related #2092641: node_help() does not allows to edit node translations

ekes’s picture

menu_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. Hence taxonomy_term_is_page() tries to access id on non-object $page_term.

ekes’s picture

catch’s picture

Priority: Major » Critical
xjm’s picture

Issue tags: +beta blocker

That's a beta blocker, so this is too.

tim.plunkett’s picture

Issue tags: +MenuSystemRevamp
Berdir’s picture

Status: Active » Fixed

#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.

xjm’s picture

Status: Fixed » Closed (duplicate)

Better status then. Thanks!

xjm’s picture

Issue tags: -beta blocker