Drupal 7 profile menu

There 2 methods to add a menu in Drupal, hook_menu or hook_menu_alert

We can define a menu like this:

  $items['user/%user/facebook'] = array(
    'title' => 'Facebook',
    'description' => 'Facebook Profile',
    'page callback' => 'facebook_user_settings',
    'file' => 'facebook.pages.inc',
    'page arguments' => array(1),
    'access callback' => 'facebook_user_access', //access arguments don't support multiple arguments, so create our access handler
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
  );

Because the menu type is MENU_LOCAL_TASK, this will add a new tab “Facebook” right after the “edit” tab.

But how to add the “facebook” tab in secondary level, after “Account”, just do as the “Linkedin” tab?

The following code is NOT right:

  $items['user/%user/edit/facebook'] = array(
    'title' => 'Facebook',
    'description' => 'Facebook Profile',
    'page callback' => 'facebook_user_settings',
    'file' => 'facebook.pages.inc',
    'page arguments' => array(1),
    'access callback' => 'facebook_user_access', //access arguments don't support multiple arguments, so create our access handler
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
  );

This will lead some errors. You should do as the following steps, (Linkedin codes):

Step 1, show a tab menu after “Account”:

function linkedin_user_categories() {
  return array(
    array(
      'name' => 'linkedin',
      'title' => 'Linkedin',
      'weight' => 3,
    ),
  );
}

Step 2, define the menu callback functions:

function linkedin_menu_alter(&$callbacks) {
  $callbacks['user/%user_category/edit/linkedin']['page callback'] = 'linkedin_user_settings';
  $callbacks['user/%user_category/edit/linkedin']['module'] = 'linkedin';
  $callbacks['user/%user_category/edit/linkedin']['page arguments'] = array(1);
  $callbacks['user/%user_category/edit/linkedin']['file'] = 'linkedin.pages.inc';
}

There is no document about hook_user_categories for Drupal 7, so this article shows How to use it.

Hope to help you.

Related articles

订阅这个博客:

关注我的微博:

关注我的推特:

本博客定期会发布价值比较高的私密文章,只需要 订阅邮件列表