views:

123

answers:

1

Is there a way to set a limit on how many menu items users can add to Primary Links menu? I'm working on a Drupal site and I have a horizontal primary links nav bar. There is only room for no more than 7-8 links in the nav bar. I don't want the future maintainer of the site to add more than 8 items to the menu. Is there a way I can set a limit on that? Some module or override function? Thanks,

+3  A: 

You could try this:

http://api.drupal.org/api/function/menu_primary_links/6

Then, using hook_form_alter, do:

$menu_links = count(menu_primary_links());

if ($menu_links > 8) {
   unset($form['menu']);
}

But, we must also protect nodes that are already in the menu. So,

$menu_links = count(menu_primary_links());

if ($menu_links > 8 && !($form['menu']['mlid']['#value'] != 0 && $form['menu']['#item']['menu-name'] == 'primary-links')) {
   unset($form['menu']);
}

That will remove the menu option from a node form only if that node has no existing menu entry in primary links menu. It checks by looking to see if the node you are editing has an mlid, and if so, if it is in the primary links menu.

hook_form_alter http://api.drupal.org/api/function/hook_form_alter

But how will our users know what happened? Lets tell them.

if ($menu_links > 8 && !($form['menu']['mlid']['#value'] != 0 && $form['menu']['#item']['menu-name'] == 'primary-links')) {
   unset($form['menu']);
   drupal_set_message('The maximum limit of links in the primary menu has been reached.', 'status', FALSE);
}

You could expand on that message by listing $menu_links too, so the user knows which nodes need removing before other nodes can be added.

Also, this is a little tricky if they make use of secondary links or other menus. In which case, you would need more programming to replace the tree within the menu options, but thats a bit more involved at the moment. They could always add nodes to secondary menus through Admin > Build > Menus.

Kevin
This is a good answer. For the sanity of the future maintainer, it's probably also worth setting a message here explaining this maximum primary link feature.
lazysoundsystem
True. Updated my answer.
Kevin
Yay! Thanks Kevin, it worked like a charm!
ninusik