Problem/Motivation
If you have menu links on multiple levels they will fall to the first level on cache rebuild. This happens only for the menu links that are defined to link for a url. Issue doesn't exist for menu links linking for a node where autocomplete functionality has been used.
Proposed resolution
This seems to be caused by this piece of code in \Drupal\Core\Menu\MenuTreeStorage:
protected function saveRecursive($id, &$children, &$links) {
if (!empty($links[$id]['parent']) && empty($links[$links[$id]['parent']])) {
// Invalid parent ID, so remove it.
$links[$id]['parent'] = '';
}
}
Remaining tasks
-
User interface changes
-
API changes
-
Comments
Comment #1
sumitmadan commentedI found that issue is here in rebuild(array $definitions) method :
Comment #2
sumitmadan commentedYeah the issue was on both the places. Created a patch hope this will work. :)
Comment #3
mark. commentedApplied patch and manually tested against a menu with a hierarchy 2, 3, and 4 levels deep. It works perfectly on my end. Nice work!
Comment #4
lauriiiStill needs tests
Comment #5
lauriiiSo apparently because of the head wasn't broken (automated tests were passing), there wasn't test coverage for this exact issue and for that reason it has to be created so that the bug doesn't occur in future, or if it does it can be noticed because of failing tests.
Comment #6
sumitmadan commentedCreating a test case.
Comment #7
sumitmadan commentedWritten a test case "MenuParentChildRelationshipTest".
Comment #8
lauriiiTest overally looks good. Could you post a only test patch to see it is failing before the fix? These things shouldbe still checked:
Is this logic around the removed line still necessary? Does the comment still apply?
There is still mixing of old and new array syntax. Maybe we should use only the new syntax?
Comment has to be made shorter since its over 80 characters long
Comment #9
sumitmadan commentedUploaded test only to check fail test.
Comment #10
sumitmadan commentedOops!! Sorry.
Comment #11
sumitmadan commentedUploading the patch with short array syntax and comments fixing.
Reason to remove the following code :
$links[$id]['parent'] = '';is that because it was setting all children's parent to blank string. The condition to unset parent is already checked in "saveRecursive" method.
Comment #13
lauriiiI think there is something wrong on the last patch? Seems like some of the changes are twice there or something?
Comment #14
sumitmadan commentedI applied this patch and worked fine. Actually this is created using
git format-patch master --stdout > patchname.patch
Command. Which creates the patch like this.
Comment #15
tim.plunkettPlease use git diff, format-patch is much harder to parse for a human.
Also, please revert the unnecessary array() to [] changes. We all prefer [] but that's out of scope for this issue.
Comment #16
lauriiitim.plunkett: Thats what was the weird part on the patch. That file is created in that issue so its all new code and for that reason I wanted the array syntax to be the new one. On the patch it looks like its changing already existing file so its like patch and interdiff in one file?
Comment #17
sumitmadan commentedUpdated the patch with git diff command.
Comment #18
sumitmadan commentedUsed Menu::create method instead of entity_create.
Comment #19
tim.plunkettAs a web test, this test takes 18 seconds. Rewritten to be a kernel test, it takes 2 seconds.
This was what I was trying to describe on IRC, sorry for just jumping in and doing it.
Comment #20
dawehnerI know that we have an issue for that already ... and another one I can't find at the moment ... #2477337: Saving a menu item with internal path instead of entity reference loses hierarchy
Note: We need a better issue title here, its not helpful at all
That totally makes sense.
Do you mind moving this directly to MenuTreeSTorageTest? It is already a kernel Test ....
seems not really needed here ...
it would be great if we could just use test entity types here?
Comment #21
sumitmadan commentedMoved testParentChildRelationshipOnMenuRebuild() to MenuTreeSTorageTest.
Please let me know why following code is not needed? Without adding a child how we will test parent child relationship?
Comment #22
dawehnerI was more talking about the need to create a node.
Comment #23
sumitmadan commentedRemoved extra line of code.
Comment #24
dawehnerAdding tag to explain what is going on.
Comment #25
catchComment #26
dawehnerIsn't that issue pretty much the same as #2468713: Internal/custom menu links force-reset to the top of their menus on cache rebuild ?
Breaking link hierarchy is the same problem on the other issue so moving this to critical as well.
Comment #28
alexpott#2468713: Internal/custom menu links force-reset to the top of their menus on cache rebuild has changed some of the same lines - so this will need a reroll and confirmation that it is still an issue.
Comment #29
sumitmadan commentedI confirms that this is still and issue. I will update the patch.
Comment #30
sumitmadan commentedJust checked. Patch applied cleanly. I believe there is no need to update the patch. Moving this again to needs review.
Comment #31
dawehnerThis is a test only patch, I'm curious whether this still fails after the other issue.
If this doesn't fail, we are safe and can mark the issue as fixed.
Comment #32
jibranWhy not add this test anyway.
Should be online.
After rebuild sounds much better.
Comment #33
dawehnerTrue, would be nice to not rely on nodes but rather on entity_test entities for example though.
testParentChildRelationshipOnMenuRebuildIt would be great to somehow communicate in the title that this is about parent entries being something else than static menu links.
Comment #35
dawehnerReupload the patch, might have been a random failure.
Comment #36
dawehnerThis test is really basically the same as the new test in #2468713: Internal/custom menu links force-reset to the top of their menus on cache rebuild IMHO we could mark it as duplicate.
What about adding the contributors of this issue onto #2477337: Saving a menu item with internal path instead of entity reference loses hierarchy, that would be nice!
Comment #37
alexpottI've added @sumitmadan and @tim.plunkett to the other commit message - if they comment on that issue I'll update the commit credit on issue too.