0

I have a module with a menu which I build like this:

in moduleName.module

function moduleName_menu() {
  $items = array(
    'title' => 'whatever',
    'page callback' => 'something_callback',
    ...
    'type' => MENU_NORMAL_ITEM
  );
  return items;
}

Now I want certain items to be disabled (Clicking them should do nothing, similar to using disabled="disabled" on a <input type="button">). I tried adding '#disabled' => true, as I would do for form items, but that didn't work. How can I disable menu items?

UPDATE

I tried using myTheme_menu_link to disable the menu items. So in the code above, I add 'disabled' => true or 'disabled' => false to certain menu items. Then, my theme_menu_link function looks like this:

function vitrine_menu_link(array $variables) {
    $element = $variables['element'];
  $sub_menu = '';

  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  if (isset($element['disabled']) && $element['disabled']) {
    error_log('Hoping to see this somewhere in the log');
    $output = '<a>' . $element['#title'] .  '</a>';
  }
  return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";    
}

I test it by emptying all caches, which rebuilds the menu, but still, the error I'm trying to log never get's logged. It seems as though my 'disabled' => true never makes it to this function...

2
  • Can you specify what you mean with "unclickable"? What is your actual goal?
    – dxc
    Commented Jan 12, 2013 at 5:33
  • @enzipher See my edit. Is that clearer?
    – Shawn
    Commented Jan 14, 2013 at 15:11

3 Answers 3

1

This can only be done on the theme layer. Try taking a look at theme_menu_link().

5
  • That means modifying drupal/includes/menu.inc. Isn't there a way to do this from within drupal/sites/all/themes/myTheme?
    – Shawn
    Commented Jan 11, 2013 at 15:11
  • 4
    @Shawn No need to modify menu.inc, you can override theme functions in your theme's template.php file with a naming convention: function MYTHEME_menu_link($vars). Once you clear the caches your version of the function will be called for any invocation of theme('menu_link');
    – Clive
    Commented Jan 11, 2013 at 15:14
  • This is not working. It looks like the key=>value pair I'm adding to my menu items is no longer there when I get to myTheme_menu_link. I will edit my question to show some code.
    – Shawn
    Commented Jan 11, 2013 at 21:30
  • How are you adding your key=>value to the item before it goes to the theme function? This would need to be done in a preprocess function. Or you could just do the logic in the theme function itself.
    – 2pha
    Commented Jan 13, 2013 at 1:41
  • I'm adding the key=>value pair in moduleName.module when creating the menu. It seems this is a mistake anyways since that code is only executed once when installing the module, not everytime a page with that menu is loaded (which is what I would need)
    – Shawn
    Commented Jan 14, 2013 at 15:22
1

As far as I know you can't disable links in that way. Depending on what you want to do I believe your options are either to add a class, programmatically or by using the Menu attributes module, and then use jQuery to disable the link, or not linking the menu item at all.

First option
Add a class or an id to the link, and do something like this to disable the link:

$('#linkid').click(function(e) {
  e.preventDefault();
});

Second option
Use the Special menu items module with the <nolink> option, which results in a menu item that is not linked, i.e. no <a> tag. You could of course do this programmatically as well, but the module is rather simple and straight forward.

This is more general solutions for menu items, which I realize it's not exactly what you are asking (a solution to your code), but it does provides solutions for your question "Can I disable module menu items?".

0

You could use the nifty menu_attributes contributed project. (You can download it from here) or just hook_menu_link_alter() (documentation) around which that module is built.

Both these approaches allow you to add attributes (including classes) to menu links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.