3

How do I format an array item using hook_menu() that links to /user but has a new title and is only accessible to logged in users?

Currently I'm using

    $items['user'] = array(
      'title' => 'Account Settings',
      'path' => 'user',
      'router_path' => 'user',
      'description' => 'Manage your username, password and contact information here.',
      'access callback' => 'user_is_logged_in',
      'type' => MENU_NORMAL_ITEM,
      'weight' => -20,
    );

Then end goal is to have a link that is titled 'Account Settings' that links to /user and that is only available to logged in users.

Edit: I guess to clarify, how can I incorporate the system menu /user item into my modules menu without using the UI?

2
  • What do you mean by "my modules menu"?
    – avpaderno
    Commented Jun 25, 2011 at 13:04
  • Sorry, that was confusing. :) I meant 'in the menu created by my custom module.'
    – Citricguy
    Commented Jul 8, 2011 at 0:14

3 Answers 3

1

If you want to duplicate the /user path, then you can use define the following menu item in the implementation of hook_menu() in your module:

  $items['your module path'] = array(
    'title' => 'User account', 
    'description' => 'Manage your username, password and contact information here.',    'title callback' => 'user_menu_title', 
    'page callback' => 'user_page', 
    'access callback' => 'user_is_logged_in',
    'type' => MENU_NORMAL_ITEM, 
    'file' => 'user.pages.inc',
    'file path' => drupal_get_path('module', 'user'),
  );

Replace "your module path" with the path used by your module, which cannot be the path already used by another module.

If you just want to alter the existing /user path, and avoid is accessible to everybody, then you need to implement hook_menu_alter() using code similar to the following:

function mymodule_menu_alter(&$items) {
  if (isset($items['user'])) {
    $items['user']['access callback'] = 'user_is_logged_in';
  }
}
1
  • Absolutely awesome. I didn't realize you could set the file like that. Thank you.
    – Citricguy
    Commented Jun 25, 2011 at 22:41
2

To move an existing menu item to a different menu in code, you can implement hook_menu_alter() and change menu_name and weight for $items['user'].

1

$items['user'] is already defined by user.module - menu items must be unique, so you can't redeclare it in your hook.

Is there a reason you can't use the "My Account" item supplied by the user module? You can override the title on the menu admin page.

6
  • That would work perfectly. Only I would need to be able to bring that link into my custom menu (say, at a weight of -20) that would be available upon enabling my custom module. How can I insert $items['user'] into my custom menu? Thank you by the way. :)
    – Citricguy
    Commented Jun 24, 2011 at 23:17
  • Simply edit the My account link and set your custom menu as the parent. That's it. You do not need to mess with hook_menu() with move/add/alter menu links to existing router items.
    – Berdir
    Commented Jun 24, 2011 at 23:28
  • Can I do this through a module rather than the UI?
    – Citricguy
    Commented Jun 24, 2011 at 23:30
  • 2
    You can implement hook_menu_alter() and then just set the menu_name and weight for $items['user'], yes. See api.drupal.org/api/drupal/modules--system--system.api.php/…
    – Berdir
    Commented Jun 25, 2011 at 0:09
  • @Berdir That is exactly what I was looking for. Would accept as answer if I could. ;)
    – Citricguy
    Commented Jun 25, 2011 at 0:41

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.