1

How can I programatically remove/hide a menu item when the menu is displayed?

  • I just went through that problem, and i ended abandoning taxonomy menu to make a custom menu based on the current language. It will probably be faster than trying to make the module work for you. – silkAdmin Oct 7 '11 at 18:14
2

If I understand well your question, you can use theme_menu_item_link() function to your template.php of your theme.

This is an example that I have tested (in this example Garland is the theme used, so you must replace the name of the function with the respective name):

Here is the example menu:

screenshot

This is the function to remove the items:

function garland_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  if ($link['title'] != 'item 1') {
    $link['localized_options'] += array('html' => TRUE);
    return l('<span>' . $link['title'] . '</span>', $link['href'], $link['localized_options']);
  }
  else {
    unset($link);
  }
}

screenshot

Please note that I have added if($link['title'] != 'item 1'){.. to detect the menu item, and remove it (with unset($link)).

This is the code for changing the text:

function garland_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  if ($link['title'] == 'item 1') {
    $link['title'] = 'new text';
  }

  $link['localized_options'] += array('html' => TRUE);
  return l('<span>' . $link['title'] . '</span>', $link['href'], $link['localized_options']);
}

screenshot

Remember to clear the cache to see the changes.

I found these other resources related with theme_menu_item_link(): http://drupal.org/node/245156, http://drupal.org/node/249864, http://drupal.org/node/417482.

  • Thanks, I just want to add that for filtering links by language just use: $link['options']['langcode'] == $language->language – Codium Oct 8 '11 at 10:17
1

Here you have my implementation. I added the return line into the last else, and check for a specific role.

function batuka_menu_item_link($link) {
  global $user;
  if (in_array("club member", $user->roles)) {
    if (empty($link['localized_options'])) {
      $link['localized_options'] = array();
    }
    if ($link['title'] != 'Instructor Profile') {
      $link['localized_options'] += array('html'=>true);
      return l('<span>'.$link['title'].'</span>', $link['href'], $link['localized_options']);
    }
    else {
      unset($link);
      return;
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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