? menu_block-path_lookup.patch Index: menu_block.admin.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/menu_block/menu_block.admin.inc,v retrieving revision 1.29 diff -u -p -r1.29 menu_block.admin.inc --- menu_block.admin.inc 13 Aug 2009 20:57:49 -0000 1.29 +++ menu_block.admin.inc 25 Sep 2009 14:14:31 -0000 @@ -115,7 +115,7 @@ function _menu_block_block_list() { * string The title of the block */ function _menu_block_format_title($delta) { - list($menu_name, $parent_mlid) = split(':', variable_get("menu_block_{$delta}_parent", ':0')); + list($menu_name, $parent_mlid, $parent_link_path) = split(':', variable_get("menu_block_{$delta}_parent", ':0')); $menus = menu_block_get_all_menus(); if (empty($menu_name) || empty($menus[$menu_name])) { $title = t('Unconfigured menu block'); @@ -128,7 +128,7 @@ function _menu_block_format_title($delta // Show the configured levels in the block info $replacements = array('@menu_name' => $menus[$menu_name], '@level1' => $level, '@level2' => $level + $depth - 1); if ($parent_mlid) { - $parent_item = menu_link_load($parent_mlid); + $parent_item = _menu_block_parent_load($menu_name, $parent_mlid, $parent_link_path, $delta); $replacements['@menu_name'] = $parent_item['title']; } if ($follow) { @@ -163,9 +163,12 @@ function _menu_block_format_title($delta function _menu_block_block_configure($delta) { // Get the list of menus. $menus = menu_block_get_all_menus(); - // Get the parent item defaults. - $parent_default = variable_get("menu_block_{$delta}_parent", 'primary-links:0'); - list($menus_default, ) = split(':', $parent_default); + // Get the parent item defaults. Reload the item, in case mlid is no longer valid. + list($menus_default, $parent_mlid, $parent_link_path) = split(':', variable_get("menu_block_{$delta}_parent", 'navigation:0')); + $menu_item = _menu_block_parent_load($menus_default, $parent_mlid, $parent_link_path); + if ($menu_item) { + $parent_default = "$menus_default:".$menu_item['mlid']; + } // Build the standard and jquery versions of the parent item options. $parent_options = $parent_options_js = array(); @@ -301,8 +304,11 @@ function _menu_block_block_save($delta, if ($edit['follow'] && !empty($edit['follow_parent'])) { $edit['follow'] = $edit['follow_parent']; } + //Load the menu path before saving + list($menu_name, $parent_mlid) = split(':', $edit['parent']); + $menu_item = menu_link_load($parent_mlid); + variable_set("menu_block_{$delta}_parent", $edit['parent'].':'.$menu_item['link_path']); variable_set("menu_block_{$delta}_title_link", $edit['title_link']); - variable_set("menu_block_{$delta}_parent", $edit['parent']); variable_set("menu_block_{$delta}_level", $edit['level']); variable_set("menu_block_{$delta}_follow", $edit['follow']); variable_set("menu_block_{$delta}_depth", $edit['depth']); Index: menu_block.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/menu_block/menu_block.module,v retrieving revision 1.53 diff -u -p -r1.53 menu_block.module --- menu_block.module 14 Aug 2009 16:26:33 -0000 1.53 +++ menu_block.module 25 Sep 2009 14:14:31 -0000 @@ -126,6 +126,59 @@ function menu_block_block($op = 'list', } /** + * Equivalent to menu_link_load, but selects by link_path and menu name. + * + * @param $link_path + * string Link path of the item to load. + * @param $menu_name + * string Name of the menu to load from. (Same link may be in multiple menus) + * + * @return + * array Database row for menu_links left joined menu_router. (Translated) + */ +function _menu_block_link_load_by_link_path($link_path, $menu_name) { + if ($item = db_fetch_array(db_query("SELECT m.*, ml.* FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path WHERE ml.link_path = '%s' AND ml.menu_name = '%s'", $link_path, $menu_name))) { + _menu_link_translate($item); + return $item; + } + return FALSE; +} + +/** + * Loads the parent menu item based on $parent_mlid or $parent_link_path. + * + * If fetching by mlid fails but fetching by $parent_link_path succeeds, + * saves the result in variables so that the next fetch needs only 1 query. + * + * @param $menu_name + * string Name of the menu to load the link from. + * @param $parent_mlid + * int mlid (menu link id) of the item to fetch. + * @param $parent_link_path + * string Link path of the link. Used if fetching by $parent_mlid fails. + * @param $delta + * int Delta of the menu block. If specified and mlid not found, the altered entry will be saved. + * + * @return + * array Translated menu item. + */ +function _menu_block_parent_load($menu_name, $parent_mlid, $parent_link_path = NULL, $delta = NULL) { + if ($parent_mlid || $parent_link_path) { + $parent_item = menu_link_load($parent_mlid); + if (!$parent_item && $parent_link_path) { + // MLID not found - check if path is found (eg. after rebuilding Taxonomy Menu) + $parent_item = _menu_block_link_load_by_link_path($parent_link_path, $menu_name); + if ($parent_item && is_numeric($delta)) { + //Save the new mlid for better performance on next fetch (1 query instead of 2) + $parent_mlid = $parent_item['mlid']; + variable_set("menu_block_{$delta}_parent", "$menu_name:$parent_mlid:$parent_link_path"); + } + } + } + return $parent_item; +} + +/** * Returns the 'view' $op info for hook_block(). * * @param $delta @@ -135,7 +188,7 @@ function _menu_block_block_view($delta) $data = array(); // Get the block configuration options. - list($menu_name, $parent_mlid) = split(':', variable_get("menu_block_{$delta}_parent", 'primary-links:0')); + list($menu_name, $parent_mlid, $parent_link_path) = split(':', variable_get("menu_block_{$delta}_parent", 'navigation:0')); $level = variable_get("menu_block_{$delta}_level", 1); $follow = variable_get("menu_block_{$delta}_follow", 0); $depth = variable_get("menu_block_{$delta}_depth", 0); @@ -178,7 +231,8 @@ function _menu_block_block_view($delta) // Prune the tree along the active trail to the specified level. if ($level > 1 || $parent_mlid) { if ($parent_mlid) { - $parent_item = menu_link_load($parent_mlid); + $parent_item = _menu_block_parent_load($menu_name, $parent_mlid, $parent_link_path, $delta); + $context['parent_mlid'] = $parent_item['mlid']; menu_block_set_title($parent_item); menu_tree_prune_tree($tree, $level, $parent_item); }