1

I am trying to manually create a menu link item in the User Menu that contains a wildcard, But I cant make it work. I can get it to work if I have a static path, but I cant get it to work with wildcards args

function hook_menu() {
  $items['user/%user/bookmarks'] = array(
    'title' => 'My Bookmarks',
    'description' => 'Link to My Bookmarks',
    'page arguments' => array(1),
    'access callback' => 'user_is_logged_in',
    'type' => MENU_NORMAL_ITEM,
    'menu_name' => 'user-menu'
  );

  return $items;
}
2
  • Hello, and welcome to Drupal Answers. What do you mean by, "manually create"? How are you trying to create the menu item?
    – apaderno
    Dec 12, 2012 at 4:20
  • Through function hook_menu in a custom module.
    – joe
    Dec 12, 2012 at 4:31

2 Answers 2

1

When I tried to use user wildcards first, I too came across this problem; and after some trial and error, found that the %user wildcard doesn't work with the menu type MENU_NORMAL_ITEM, instead it works if the type is MENU_LOCAL_TASK.

So, the code should be:

function hook_menu() {
  $items['user/%user/bookmarks'] = array(
    'title' => 'My Bookmarks',
    'description' => 'Link to My Bookmarks',
    'page arguments' => array(1),
    'access callback' => 'user_is_logged_in',
    'type' => MENU_LOCAL_TASK,
    'menu_name' => 'user-menu'
  );

  return $items;
}

Usually, this menu type is rendered as a tab in user menu.

I didn't get enough time to figure out why it doesn't work with MENU_LOCAL_TASK, but when I do find the reason, I'll post it back.

2
  • 1
    Interesting, thank you. This is a link to a view, and I already have a Tab to it configured in the view...but you cant add more than one menu item in the view, so Ill just reverse it. Ill put the Menu Item link in the view, and use MENU_LOCAL_TASK for the tab.
    – joe
    Dec 12, 2012 at 4:43
  • 1
    It works for user/%user, and that is a MENU_NORMAL_ITEM menu item, as its type is not explicitly defined. See user_menu().
    – apaderno
    Dec 12, 2012 at 4:43
1

You forgot to define the page callback for that menu item, which means Drupal will use the one for the parent menu item, as reported in the hook_menu() documentation.

"page callback": The function to call to display a web page when the user visits the path. If omitted, the parent menu item's callback will be used instead.

As the parent menu item for user/%user/bookmarks is user/%user, this means that, instead of showing the page you want, Drupal is showing the user profile page.

As you are creating a menu item for a view, there is normally no need to implement hook_menu(), as a view can be associated with a menu item.

screenshot

If you need to show the view using more than one menu item, just clone the page, and change the path for the cloned page. With two pages, you will be able to use two menu items; with three pages, you will be able to use three menu items.

screenshot

5
  • does this mean that if 'page callback' is specified the menu items can be rendered as MENU_NORMAL_ITEM? I'm talking specifically when we use %user wildcards.
    – AjitS
    Dec 12, 2012 at 5:01
  • The overhead of cloning a view is too high, just for adding 1 menu item. One more view is one more export, which means unnecessary code being in version control
    – joe
    Dec 12, 2012 at 5:04
  • I read that wrong. I thought you were saying to clone the view, but you said clone the page. But still, cloning the page just for a menu item seems really unnecessary.
    – joe
    Dec 12, 2012 at 5:08
  • 1
    @indrock I tried with node/%node/bookmarks, MENU_NORMAL_ITEM, and "navigation" as menu name. The link appears, but it points to node/bookmarks. I tried with user/%/bookmarks, and the link doesn't appear between the links I can enable for the "navigation" menu. The strange is that blog/%user_uid_optional (used from the Blog module) appears, and links to the correct page. It is more complicated than "if the menu path contains %user, it doesn't work."
    – apaderno
    Dec 12, 2012 at 5:58
  • @kiamlaluno Thanks. This is interesting and strange at the same time.
    – AjitS
    Dec 12, 2012 at 6:20

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.