If I try to save a new menu item with menu_link_save, but specify a non-existant path, the item is not saved, but the function (menu_link_save) returns an id (it should return FALSE.

Comments

Sam Dark’s picture

Can confirm the issue.

johanneshahn’s picture

menu_link_save() function needs a valid menu item.

with the menu_form the new menu item path ($item['link_path'])
is validated with function menu_edit_item_validate()

if (!trim($item['link_path']) || !menu_valid_path($item)) {
    form_set_error('link_path', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $item['link_path'])));
  }

see: http://api.drupal.org/api/function/menu_edit_item_validate/6

if you call menu_link_save() directly you need a valid menu item.
i think its not a good solution to check valid paths twice. once at menu_edit_item_validate()
and once at menu_link_save().

alexiswatson’s picture

That assumes that menu_link_save() isn't being invoked programmatically, outside of the normal use case in which it would already be validated first. While I agree that double-handling should be avoided where it can be helped, I don't know if that's the case here. Reading the documentation, it says nothing about the params of menu_link_save() requiring validation before being passed in, and claims that FALSE is returned if the item didn't save. If it isn't returning FALSE on failure, it's a bug.

seanberto’s picture

Following to see where this goes. I agree with c.ex after stumbling to debug a similar issue.

alexiswatson’s picture

Version: 6.8 » 7.x-dev

This still appears to be an issue in D7. I'm not sure whether the bug is in the documentation ("The mlid of the saved menu link, or FALSE if the menu link could not be saved due to the maximum depth being exceeded," which doesn't make as much sense) or the code (not returning FALSE when failing to save for other reasons), but one of these is clearly incorrect. Any ideas?

spesic’s picture

Issue summary: View changes

Bumping this, menu_link_save shouldn't return mlid if the call is not successful.

Status: Active » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.