A menu link must have at least one visible (hidden = 0) child menu link for its has_children value to be set to 1, if there are no children or all the children of a menu link are hidden, has_children will be 0 - see the _menu_update_parental_status() function.
The value of has_children is used to determine whether the expanded class is added to a menu link (if has_children evaluates to TRUE, expanded is added etc), this makes perfect sense but has_children is also checked in the _menu_delete_item() function where, depending on its value, child menu links are loaded, re-parented and saved or orphaned.
- If has_children evaluates to TRUE (there is at least one child menu link and it's visible) - all child menu links regardless of visibility are loaded, re-parented and saved.
- If has_children evaluates to FALSE (there are no child menu links or all child menu links are hidden) - the process is skipped, any hidden child menu links are orphaned - their plid no longer exists.
The attached patch simply removes the has_children check in _menu_delete_item() so that regardless of the hidden status of child menu links they are re-parented correctly.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | remove_has_children_check-1258014-11.patch | 31.43 KB | matason |
| #8 | remove_has_children_check-1258014-8.patch | 30.62 KB | matason |
| #2 | remove_has_children_check-1258014-2.patch | 2.81 KB | matason |
| remove_has_children_check_menu_delete_item.patch | 1.05 KB | matason |
Comments
Comment #2
matason commentedChanged version to 7.x-dev, attached patch which now includes tests.
Comment #3
chx commentedIf this is the case why it's called has_children instead of has_visible_children? It's not an adequate answer because chx named it so! I am allowed to make mistakes too :) Looking at how has_children is used to set expanded/collapsed it'd be a more adequate name. It'd be a very easy upgrade function and a search-replace.
Comment #4
chx commentedAlso things like this go into 8.x first.
Comment #5
xjmTagging.
Comment #6
matason commentedxjm, chx thanks for your comments here and for your help on IRC, I'll work on another patch around changing the column name and I'll also add some more tests for ensuring correct handling of visible descendants on parent menu link deletion, this works as I would expect but we currently don't test it.
Comment #7
matason commentedI've been looking at the tests in modules/simpletest/tests/menu.test and in particular testMenuLinkReparenting() as that seems to be the place for additional tests for this issue...
It appears to me that testMenuLinkReparenting() is really testing the menu's ability to heal itself when bad things happen: we set up the conditions in the test and call assertMenuLinkParents() which includes a menu_link_save() on all the menu links before the assert is done. Whilst that's fine for testing the self-healing capabilities of the menu the additional menu_link_save() in the assert creates false conditions for testing just re-parenting.
So I just wanted to sanity check my thinking really, does it seem sensible to separate the test into two tests: a) tests around self-healing (with the additional menu_link_save()) and b) tests around reparenting (without the additional menu_link_save()).
Comment #8
matason commentedThe attached patch:
Comment #9
chx commentedGreat -- needs an update function but i am behind with #1182290: Add boilerplate upgrade path tests give me a few days on that.
Comment #10
matason commentedThanks @chx. Yes, update function - I'll re-roll the patch with that included.
Comment #11
matason commentedThe attached patch contains everything in #8 plus implements hook_update_N() in system.install to change menu_links.has_children to menu_links.has_visible_children.
Comment #12
sunThe schema change cannot be backported to D7. (sorry, that's obvious, don't have time for more right now.)
Comment #13
kscheirer#11: remove_has_children_check-1258014-11.patch queued for re-testing.
Comment #23
pameeela commentedThis code is long gone and it doesn't look like it's still valid in the current implementation. I also tested this manually and it worked as expected: