includes/menu.inc | 30 ++++++++++++++---------------- modules/menu/menu.test | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git includes/menu.inc includes/menu.inc index 8e2fb1f..0f386a9 100644 --- includes/menu.inc +++ includes/menu.inc @@ -2639,24 +2639,22 @@ function menu_link_save(&$item) { } } - // If we have a parent link ID, we use it to inherit 'menu_name' and 'depth'. + // If this link defines a parent link id, try to find it in the current menu + // links. If found, we use it for the re-parenting process below. This parent + // link validation also needs to be done for links specifying a plid of 0 + // (zero), since {menu_links} may contain stale/bogus data caused by a + // re-parenting process that went wrong in a previous rebuild. if (isset($item['plid'])) { - if ($item['plid']) { - $parent = db_query("SELECT * FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $item['plid']))->fetchAssoc(); - } - // If the parent link ID is zero, then this link lives at the top-level. - else { - $parent = FALSE; - } - } - // Otherwise, try to find a valid parent link for this link. - else { + $parent = db_query("SELECT * FROM {menu_links} WHERE mlid = :mlid", array(':mlid' => $item['plid']))->fetchAssoc(); + } + // If no plid was specified or it was not found, try to find a new, valid + // parent link for this link. Only do this if this is a menu link provided + // through hook_menu(). We can not guess the parent for menu items added + // through the UI or modules since we can not assume that they are parented + // by their path. + if (empty($parent) && $item['module'] == 'system') { $query = db_select('menu_links'); - // Only links derived from router items should have module == 'system', and - // we want to find the parent even if it's in a different menu. - if ($item['module'] == 'system') { - $query->condition('module', 'system'); - } + $query->condition('module', 'system'); // We always respect the link's 'menu_name'; inheritance for router items is // ensured in _menu_router_build(). $query->condition('menu_name', $item['menu_name']); diff --git modules/menu/menu.test modules/menu/menu.test index 3c99e26..45ca01a 100644 --- modules/menu/menu.test +++ modules/menu/menu.test @@ -576,7 +576,7 @@ class MenuNodeTestCase extends DrupalWebTestCase { // Change default parent item to Navigation menu, so we can assert more // easily. $edit = array( - 'menu_parent' => 'navigation:0', + 'menu_parent' => 'main-menu:0', ); $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));