You may not want to fix this one in D6, but depending on whether the menu is being created or updated, the $form_state['values']['menu_name'] differs.

When inserting a menu, say "my-menu", then that value is "my-menu".

When updating a menu, say "my-menu", then the value is set to "menu-my-menu".

And internal menus such as the "primary-links" will never have the "menu-..." prepended.

So, the patch below is required for any 3rd party module to work properly on Add menu.

We had the exact same problem with the items and the fix is exactly the same.

Thank you.
Alexis Wilke

P.S. Code from the item submission, notice the & that was added a while back.

/**
 * Process menu and menu item add/edit form submissions.
 */
function menu_edit_item_submit($form, &$form_state) {
  $item = &$form_state['values']['menu'];

  [...]
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, menu-invalid_name-6.x.patch, failed testing.

opi’s picture

Version: 6.x-dev » 7.x-dev
Status: Needs work » Needs review
FileSize
806 bytes

Still in d7

We also need to add 'menu_name' to the $item.
In my case, I got a notice with admin_language, in a hook_menu_link_alter(). (because menu_link_save() is called in menu_edit_menu_submit()).

So here is my patch.

pjmcghee’s picture

Notice: Undefined index: menu_name in admin_language_menu_link_alter() (line 64 of /var/www/html/sites/all/modules/admin_language/admin_language.module).

I get this error when I try to add a category to the aggregator module. i have installed the patch but i still get the error. the category is created but the error persists.

das-peter’s picture

Issue summary: View changes

Just stumbled over this when dealing with this: #2090403: Problem with adding a New Menu
It seems like the patch here would be the right place to fix the issue and it fixes it.
Making the adjustment in Entity menu links just a workaround.

Patch here solved the issue & looks reasonable. Starting a re-test and if it is green I'd say RTBC.

das-peter’s picture

Status: Needs review » Reviewed & tested by the community

Test's came back green. So as said RTBC.

chx’s picture

Status: Reviewed & tested by the community » Needs work

This is certainly odd but in the confines of a stable release we can't move drupal_alter('menu_link', $item); past the // Load defaults. in menu_link_save. What we can do, however, is to move that defaults array into a new private function, and add it in menu_edit_menu_submit so that menu_link_save gets a fully populated link...

Alternatively this should be a documentation fix because whatever else calls menu_link_save will not have a fully populated link and your alter implementations needs to be coded in a way to deal with that.

chx’s picture

Actually let's do both: we should populate but also recommend alterers to do a $link += _menu_link_defaults() the beginning of an alter, costs nothing.

das-peter’s picture

Status: Needs work » Needs review
FileSize
2.88 KB

@chx Thanks a lot!
First shot at chx's proposal.

chx’s picture

Assigned: AlexisWilke » Unassigned
Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs change record

I think this is a good one. Here are the possible problems:

  1. Some link alter is using the lack of certain properties to detect something. But what? Link newness? Everyone having two functioning brain cells would use mlid for that and we didn't add mlid. I can't figure a way to break here something.
  2. Someone already has a function called menu_link_defaults. Your code is broken, you get to keep both parts. That's an asinine thing to even attempt, the core has a menu module, a bazillion menu_* functions , heck quite a few menu_link_* functions, stay the hell out of those namespaces.
naveenvalecha’s picture

das-peter’s picture

@naveenvalecha Thanks for the CR! I think it contains all the required information.

chx’s picture

Issue tags: -Needs change record

Thanks for the CR, it's great! I very slightly edited it. This, however, gave me new insight -- do we really want to suggest in the API documentation to use this function? It'd create contrib modules that fatal with non-latest Drupal 7.x. I have wording for this in the CR, do we want to copy over?

naveenvalecha’s picture

-- do we really want to suggest in the API documentation to use this function?

I think yes bt I'm still new to writing the CR and other core issue queues so lets wait what @das-peter thinks ?

das-peter’s picture

I'm torn apart between having the way to handle this in the API Doc and having a probably not existing function mentioned in the API Doc.
I don't think we've a documentation notion to define something's valid just as of a certain minor version, right? At least I can't remember I've seen such a notion before.
Removed that line of documentation in the updated patch.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 15: menu-invalid_name_929176-15.patch, failed testing.

Status: Needs work » Needs review
das-peter’s picture

Status: Needs review » Reviewed & tested by the community

Fail was a hiccup back to RTBC.

das-peter’s picture

FileSize
2.9 KB

Updated documentation wording and added link to CR as per hint of chx via IRC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: menu-invalid_name_929176-19.patch, failed testing.

Status: Needs work » Needs review
das-peter’s picture

Status: Needs review » Reviewed & tested by the community

Fail was a hiccup back to RTBC.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs work

This patch doesn't seem to be fixing the original bug anymore? (Which was basically, as I understand it, that submit handlers which run after menu_edit_menu_submit() don't have any way to know that for a newly-inserted item, the menu name that was actually saved is different from what's in $form_state['values']['menu_name']. Maybe it's just up to them to know that particular detail, but another way to state the problem is that they have no access to the final saved menu/link at all, and it could have changed in various ways as a result of being saved.)

Now the issue with "Notice: Undefined index: menu_name in admin_language_menu_link_alter()" (which was brought up later, and doesn't seem related to the original issue)... I am not sure what benefit we get from fixing it only in this one instance? If any code implementing hook_menu_link_alter() is still going to have to deal with the possibility that they are getting an incomplete menu link, I don't see why passing them a complete menu link in this particular instance changes anything. Shouldn't we either fix it completely (by adding the defaults in menu_link_save() before the hook is invoked) or just leave things the way they are and document the behavior?

I am also not sure there are backwards-compatibility concerns with setting the defaults before invoking the hook. (Or if there are, why is it OK to do that in the most common case, as this patch does, but not other cases, i.e. not in menu_link_save() directly?) To be extra safe I guess we could just add that beforehand but not actually move any of the other code, i.e. continue to try adding the defaults a second time after the hook is invoked, and also leave the code that sets $item['external'] after the hook is invoked. If we did that I don't see any real backwards-compatibility concerns. If existing code were trying to rely on defaults not being set, that would be pretty broken already, right - since in the case of a programmatic menu_link_save() the defaults could already be set even now.

Also, thanks for getting started on the change notice. It looks like a good start. We'll probably have to rewrite it a bit eventually if we change direction here, and one small thing to fix while doing that is the links currently point to the wrong hook (hook_menu_alter() rather than hook_menu_link_alter()).

Echofive’s picture

FileSize
2.98 KB

Hello,

I add a new patch to be compilant with Drupal 7.58.
No changes in the code base.

Echofive’s picture

Echofive’s picture

FileSize
2.98 KB

I add a new patch to be compliant with Drupal 7.63.
No changes in the code base.