Updated: Comment #0

Problem/Motivation

blocks: #2107533: Remove {menu_router}.

Proposed resolution

Remove all uses of this function, since it loads/uses data from {menu_router}

There is the possiblity that something asked the router to rebuild. If this happens in a request where you request local tasks it is NOT rebuilt yet.
Previously the menu_get_item() code took care about rebuilding it. This patch removes menu_get_item() so we set the optional rebuilding in the local task manager to ensure a fine state of the router
when you ask for all local tasks.

Remaining tasks

TODO

User interface changes

N/A

API changes

Remove API function menu_get_item()

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Xano’s picture

core broke my module

yeah well. get used to it

Those quotes summarize #2185465: menu_get_item() does not work with hook_menu_link_defaults() pretty well.

Xano’s picture

What menu_get_item() does is try and load a menu item for a path. If it can't find an item for the given path, it removes the last path fragment and tries again.

However, we no longer deal with paths, but with routes, and menu links explicitly specify the machine names of their parent links, we no longer have an easy way to find the menu item closest to a given path, as we don't have a way to move up the tree anymore, and moving down the tree is too intensive.

dawehner’s picture

As menu_get_object() relies on menu_get_item() we need to solve that meta issue first.

catch’s picture

Status: Active » Postponed
catch’s picture

Status: Postponed » Active
dawehner’s picture

Note: This removes all calls which are obvious, but leaves the one in menu.inc, which are still part of the API workflow. These can and should be cleaned up without too many problems later.

tim.plunkett’s picture

  1. +++ b/core/modules/block/custom_block/custom_block.local_actions.yml
    @@ -10,5 +10,5 @@ custom_block_add_action:
    +    - custom_block.list
    
    +++ b/core/modules/block/custom_block/custom_block.module
    @@ -38,25 +38,6 @@ function custom_block_help($path, $arg) {
    -      $item['localized_options']['query']['destination'] = 'admin/structure/block/custom-blocks';
    

    This is why we didn't use that already. We need that destination...

  2. +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Plugin/Menu/LocalAction/CustomBlockAddLocalAction.php
    @@ -24,6 +25,10 @@ public function getOptions(Request $request) {
    +      $options['query']['destination'] = 'admin/structure/block/custom-blocks';
    

    Nevermind! LOL.

This looks great.

dawehner’s picture

Status: Active » Needs review

Let's invite the testbot as well.

Status: Needs review » Needs work

The last submitted patch, 7: menu_get_item-2177031-7.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
34.91 KB
17.86 KB

Removed a lot of the old menu_get_item() as well as the code / tests which rely on it.

Status: Needs review » Needs work

The last submitted patch, 11: menu_get_item-2177031-11.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
Issue tags: +API change
FileSize
43.03 KB
13.53 KB

This was too agressive, let's just remove the ones we actually don't need but also remove menu_get_item() itself.

Can a maintainer add the "Approved API change", as we will remove stuff like local task support?

Status: Needs review » Needs work

The last submitted patch, 13: menu_get_item-2177031-13.patch, failed testing.

dawehner’s picture

Issue summary: View changes
Status: Needs work » Needs review
FileSize
49.93 KB
6.9 KB

The general

Status: Needs review » Needs work

The last submitted patch, 15: local_task-2177031-15.patch, failed testing.

dawehner’s picture

15: local_task-2177031-15.patch queued for re-testing.

The last submitted patch, 15: local_task-2177031-15.patch, failed testing.

rlmumford’s picture

PHP Fatal error: Call to a member function rebuildIfNeeded() on a non-object in /var/lib/drupaltestbot/sites/default/files/checkout/core/lib/Drupal/Core/Menu/LocalTaskManager.php on line 193

Any ideas?

rlmumford’s picture

I've had a look and can't see anything obvious why that wouldn't be there at that point.

dawehner’s picture

Status: Needs work » Needs review
FileSize
51.1 KB
1.17 KB

Oh, some phpunit tests fail.

dawehner’s picture

21: menu_get_item-2177031-21.patch queued for re-testing.

pwolanin’s picture

Can you explain why this is needed?

+  \Drupal::configFactory()->setOverrideState(FALSE);

Also, rebuilding the routes in the middle of the manager seems a bit odd - was that a bug you ran into?

dawehner’s picture

Thank you for your reviews!

Can you explain why this is needed?

Just have a look at: #2115025: Content admin views title saved localized to the menu table, this will explain the need for it. This patch just reapplies the same pattern.

Also, rebuilding the routes in the middle of the manager seems a bit odd - was that a bug you ran into?

Yes, this was a bug, see https://qa.drupal.org/pifr/test/726203
Saving a view triggers at some point a rebuild of everything, so the router system and local tasks. The notice you see exists, because the router is not rebuilt yet at that point.
The reason why this patch fails is the removal of menu_get_item(), which had a call to

-    \Drupal::service('router.builder')->rebuildIfNeeded();

integrated.

I thought about putting this into the render code as well, as there is no route provider access during the rebuild, though it seems to be something we really want to avoid be being
triggered on the actual side, unless something complex like a local task rebuild happens.

tim.plunkett’s picture

Needed a reroll, and had some stray comments referring to menu_get_item that I removed.

+++ b/core/modules/system/lib/Drupal/system/SystemManager.php
@@ -160,7 +179,9 @@ public function getMaxSeverity(&$requirements) {
+    $route_name = $this->request->attributes->get(RouteObjectInterface::ROUTE_NAME);
+    $items = $this->menuLinkStorage->loadByProperties(array('route_name' => $route_name));
+    $item = reset($items);

Follow-up idea: Provide a dedicated method on menu link storage (or elsewhere) for loading by route name.

I think this is ready to go, but I'm going to wait for green before reRTBCing.

tim.plunkett’s picture

Status: Needs review » Reviewed & tested by the community

Great! Since my only work on this issue was doc fixing at the end, I feel okay with RTBCing this.

This is a huge blocker, great job @dawehner!

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Needs a change record draft to be RTBC.

dawehner’s picture

Status: Needs work » Reviewed & tested by the community

I don't see that we should document more, all of that code is really deprecated already anyway
https://drupal.org/node/2203305

webchick’s picture

We need some before/after code of what you're supposed to do if you were depending on menu_get_item().

dawehner’s picture

sun’s picture

Looks good to me. The existing change notice(s) should indeed be sufficient for now — developers will have to re-implement all of their code for the request/response architecture anyway.

Just the DX of retrieving the current route name or route object looks a bit ugly - wondering whether we can improve that prior to release... But that's a different issue, of course.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

menu_get_item-2177031-25.patch no longer applies.

error: patch failed: core/includes/menu.inc:431
error: core/includes/menu.inc: patch does not apply

catch’s picture

@sun the DX issue is currently part of #2124749: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API, it's incredibly ugly but it's already being tracked.

Berdir’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
54.45 KB

Re-roll, only conflicted because a drupal_alter() was changed to moduleHandler in menu_get_item(), so just re-removed that part.

catch’s picture

Added an extra note to the change notice pointing out the request attributes stuff is provisional.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 34: menu_get_item-2177031-34.patch, failed testing.

Berdir’s picture

Status: Needs work » Needs review

34: menu_get_item-2177031-34.patch queued for re-testing.

Sutharsan’s picture

Issue tags: -Needs reroll

Needs reroll tag removed.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Back to RTBC

webchick’s picture

Status: Reviewed & tested by the community » Fixed

The change notice wasn't exactly what I had in mind (more a before/after code comparison of D7 vs .D8). I tried to find one in the patch to use and came across this:

-    $menu_item = menu_get_item('menu-test/context');
+   $menu_items = \Drupal::entityManager()->getStorageController('menu_link')->loadByProperties(array('route_name' => 'menu_test.context'));

Which, um. Yikes! ;)

So given that people seem to use menu_get_item() for all manner of things, and given that we can't possibly ship with DX that bad, I think the way the current change notice is phrased is fine. Basically, if you needed stuff from menu_get_item() before, you now get it from $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT); until we improve that in #2124749: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API. This code snippet also highlights that we pretty desperately need #2190313: Add $EntityType::load() and loadMultiple() to simplify loading entities.

Since this currently applies, getting it in while it's hot!

Committed and pushed to 8.x. WOOT.

dawehner’s picture

- $menu_item = menu_get_item('menu-test/context');
+ $menu_items = \Drupal::entityManager()->getStorageController('menu_link')->loadByProperties(array('route_name' => 'menu_test.context'));
Which, um. Yikes! ;)

Well this is like comparing apples and oranges to be honest.

xjm’s picture

Issue tags: -Needs change record

Added #2124749: [meta] Stop using $request->attributes->get(MAGIC_KEY) as a public API to the change record and a comment there WRT updating it when possible.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.