API page: http://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_set_item/7

Enter a descriptive title (above) relating to menu_set_item, then describe the problem you have found:

This is my first potential issue. I will do my best to follow protocol. I may be misunderstanding the behavior of this function. If so, I apologize.

Repeatable?

I was able to repeat this in multiple page callback functions for different menu items.

What are the steps required to reproduce the bug?

I have a link in hook_menu which can change based on the page accessed.

      $items['intro-courses/course/section/course-id'] = array(
		'title' => 'Add Section',
		'page callback' => 'drupal_get_form',
		'page arguments' => array('intro_section_form'),
		'access callback' => 'intro_phys_administrator_access',
		'menu_name' => 'main-menu',
		'type' => MENU_NORMAL_ITEM,
	);

From the above code, the course-id in the path can change based on the rendered page. In the page callback of the rendered page, I call menu_set_item with the following code...


//get the cached menu item
$item = menu_get_item('intro-courses/course/section/course-id');
//debugging purposes only
dpm($item);
//retrieve its path (should be the same but sanity check)
$old_path = $item['path'];

//alter the link
$item['href'] = 'intro-courses/course/section/add/'.arg(2);
//change the path
$item['path'] = 'intro-courses/course/section/add/'.arg(2);
//update the map
$item['map'][3] = arg(2);
//doubtful i need to update the original map, but doing this for testing
$item['original_map'][3] = arg(2);
//updating the title of the link
$item['title'] = 'Please change';

//attempting to update the menu item
menu_set_item($old_path, $item);

/**

//the following line seems to work as expected when no path is passed
menu_set_item(NULL, $item); 

**/

What behavior were you expecting?

I was expecting that the page callback function would update the altered attributes menu item specified by the path parameter before page load. It did not seem to change anything. I was able to successfully overwrite the object when the path parameter was set to NULL.

I have the children menu items in main-menu rendering in the theme. The menu item I am trying to alter is a child item of the rendered page, but changes based on the rendered page.

What happened instead?

The rendered page left the specified menu item unchanged.

Comments

disasm’s picture

I've confirmed this issue occurs in 8.x as well. Here's a some code for a test module that creates two menu paths. When you visit the one, it should modify the menu entry (path/title) for the other one. It results in the menu not being changed.

http://codepad.org/UBwUPxnU