Drupal - Set 'user/%/edit/uprofile' to def

2020-07-25 08:47发布

I'm using the content profile module. When a user is on their view profile page and press edit, they expect the profile edit page to show, not account settings as it is now. The path to content profile edit page is 'user/%/edit/uprofile'. Does anyone know how to set the 'user/%/edit/uprofile' to default tab for 'user/%/edit'?

标签: php drupal menu
4条回答
\"骚年 ilove
2楼-- · 2020-07-25 09:27

Edit 2 (for Content Profile)

This example is for a Content Profile tab. Just change $type to the short name of the content type for the profile. This also shows how to clean up the code for real-world use (my previous examples were really, really verbose):

function mymodule_menu_alter(&$items) {
  // Specify the content profile type you'd like to work with
  $type = 'profile';

  // Make sure the user has a Content Profile to edit
  if (!empty($items['user/%user_category/edit/' . $type])) {
    // Pull out the menu items we want to modify.
    $account  = &$items['user/%user_category/edit/account'];
    $edit     = &$items['user/%user_category/edit'];
    $profile  = &$items['user/%user_category/edit/' . $type];

    // Specify the Edit Account page as just a regular tab. 
    // You do not need to change this block: this will always be the same as long 
    // as you don't want Edit account to be the default tab.
    $account = array(
      'type' => MENU_LOCAL_TASK,
      'page callback' => $edit['page callback'],
      'page arguments' => $edit['page arguments'],
      'access callback' => $edit['access callback'],
      'access arguments' => $edit['access arguments'],
      'module' => $edit['module'],
      'file' => $edit['file'],
    ) + $account;

    // Change the default action to take when hitting user/<UID>/edit to
    // the content profile
    $edit = array(
      'page callback' => $profile['page callback'],
      'page arguments' => $profile['page arguments'],
      'access callback' => $profile['access callback'],
      'access arguments' => $profile['access arguments'],
      'file' => $profile['file'],
      'file path' => $profile['file path'],
    ) + $edit;

    // Specify the profile page as the default tab and remove settings
    // already set above
    $profile['type'] = MENU_DEFAULT_LOCAL_TASK;
    unset($profile['page callback'], 
      $profile['page arguments'], 
      $profile['access callback'], 
      $profile['access arguments'], 
      $profile['file'], 
      $profile['file path']);
  }
}

Edit 1 (for Core's Profile module)

I didn't realize you wanted to change the default tabs under Edit. It's the same general principle as I described below, but with some minor modifications. This example will make the Personal tab (at user/<UID>/edit/Personal) default instead of the account tab:

function mymodule_menu_alter(&$items) {
  // Specify the Edit Account page as just a regular tab. 
  // You do not need to change this block: this will always be the same as long 
  // as you don't want Edit account to be the default tab.
  $items['user/%user_category/edit/account']['type'] = MENU_LOCAL_TASK;
  $items['user/%user_category/edit/account']['page callback'] = $items['user/%user_category/edit']['page callback'];
  $items['user/%user_category/edit/account']['page arguments'] = $items['user/%user_category/edit']['page arguments'];
  $items['user/%user_category/edit/account']['access callback'] = $items['user/%user_category/edit']['access callback'];
  $items['user/%user_category/edit/account']['access arguments'] = $items['user/%user_category/edit']['access arguments'];
  $items['user/%user_category/edit/account']['module'] = $items['user/%user_category/edit']['module'];
  $items['user/%user_category/edit/account']['file'] = $items['user/%user_category/edit']['file'];

  // Change default action to take when hitting user/<UID>/edit to
  // the settings of the page you want to use.
  // -- Custom settings start here --
  $items['user/%user_category/edit']['page callback'] = $items['user/%user_category/edit/Personal']['page callback'];
  $items['user/%user_category/edit']['page arguments'] = $items['user/%user_category/edit/Personal']['page arguments'];
  $items['user/%user_category/edit']['access callback'] = $items['user/%user_category/edit/Personal']['access callback'];
  $items['user/%user_category/edit']['access arguments'] = $items['user/%user_category/edit/Personal']['access arguments'];
  $items['user/%user_category/edit']['module'] = $items['user/%user_category/edit/Personal']['module'];
  $items['user/%user_category/edit']['file'] = $items['user/%user_category/edit/Personal']['file'];

  // When loading a profile tab, user_edit needs two parameters. The second parameter is the name of the profile
  // (i.e. Personal from user/<UID>/edit/Personal).
  $items['user/%user_category/edit']['page arguments'] = array(1, 'Personal');

  // Specify the Personal page as the default tab and remove settings
  // already set above */
  $items['user/%user_category/edit/Personal']['type'] = MENU_DEFAULT_LOCAL_TASK;
  unset($items['user/%user_category/edit/Personal']['page callback']);
  unset($items['user/%user_category/edit/Personal']['page arguments']);
  unset($items['user/%user_category/edit/Personal']['access callback']);
  unset($items['user/%user_category/edit/Personal']['access arguments']);
  unset($items['user/%user_category/edit/Personal']['module']);
  unset($items['user/%user_category/edit/Personal']['file']);
}

Overview and Concept

You can do this with hook_menu_alter and changing the types for specific tabs.

Changing the default tab is a little bit of a harrowing process. Basically, the default tab inherits all the properties of the page without any tabs selected. This allows a user to go to user/UID and get the view page without having to go directly to user/UID/view.

To get a clearer understanding of this, check out the user_menu() hook implementation. Note how $items['user/%user/view'] is pretty empty, and $items['user/%user_uid_optional'] contains all the settings you would've expected to see under $items['user/%user/view'].

So, you're going to first set up the view tab to act as a regular tab: to do this, you're going to have to copy all the settings that are attached to the user/UID menu item and put them into the user/UID/view menu item.

Once you do that, you're going to replace the settings for user/UID with the settings for the tab you want to become the default tab.

Finally, you're going to unset all the menu items for the default tab since it will inherit the settings for user/UID.

Check out this code which makes the Edit tab default:

function mymodule_menu_alter(&$items) {
  // Specify the View page as just a regular tab. 
  // You do not need to change this block: this will always be the same as long 
  // as you don't want View to be the default tab.
  $items['user/%user/view']['type'] = MENU_LOCAL_TASK;
  $items['user/%user/view']['page callback'] = $items['user/%user_uid_optional']['page callback'];
  $items['user/%user/view']['page arguments'] = $items['user/%user_uid_optional']['page arguments'];
  $items['user/%user/view']['access callback'] = $items['user/%user_uid_optional']['access callback'];
  $items['user/%user/view']['access arguments'] = $items['user/%user_uid_optional']['access arguments'];
  $items['user/%user/view']['file'] = $items['user/%user_uid_optional']['file'];

  // Normal tabs don't have a weight
  unset($items['user/%user/view']['weight']);

  // Change default action to take when hitting user/<UID> to
  // the settings of the page you want to use.
  // -- Custom settings start here --
  $items['user/%user_uid_optional']['page callback'] = $items['user/%user_category/edit']['page callback'];
  $items['user/%user_uid_optional']['page arguments'] = $items['user/%user_category/edit']['page arguments'];
  $items['user/%user_uid_optional']['access callback'] = $items['user/%user_category/edit']['access callback'];
  $items['user/%user_uid_optional']['access arguments'] = $items['user/%user_category/edit']['access arguments'];
  $items['user/%user_uid_optional']['file'] = $items['user/%user_category/edit']['file'];

  // Specify the Edit page as the default tab and remove settings
  // already set above
  $items['user/%user_category/edit']['type'] = MENU_DEFAULT_LOCAL_TASK;
  $items['user/%user_category/edit']['weight'] = -10;
  unset($items['user/%user_category/edit']['page callback']);
  unset($items['user/%user_category/edit']['page arguments']);
  unset($items['user/%user_category/edit']['access callback']);
  unset($items['user/%user_category/edit']['access arguments']);
  unset($items['user/%user_category/edit']['file']);
}

Replace the second part of the function with the settings for your menu item and you should be in good shape. Of course, remember to clear the cache after making any menu changes for them to take effect.

查看更多
Bombasti
3楼-- · 2020-07-25 09:34

I was searching for a solution but wasn't satisfied with such complexity. I did it like this.

function MYTHEME_menu_link(array $variables) {
    global $user;

    if($variables["element"]["#theme"] == "menu_link__user_menu" && $variables["element"]["#original_link"]["router_path"] == "user") {
        $variables["element"]["#href"] = "user/".$user->uid."/edit";
    }

    return theme_menu_link($variables);
}
查看更多
爷、活的狠高调
4楼-- · 2020-07-25 09:35

There is a module on drupal.org. Works with profile fields. See Profile Tools.

查看更多
仙女界的扛把子
5楼-- · 2020-07-25 09:54

Here's the code that worked for me with content profile installed and the default content type ('profile'):

function mymodule_menu_alter(&$items) {
  // Save the data from user/%/edit into user/%/edit/account.
  $items['user/%user_category/edit/account'] = $items['user/%user_category/edit'];
  $items['user/%user_category/edit/account']['tab_parent'] = 'user/%/edit';
  $items['user/%user_category/edit/account']['title'] = 'Account';

  // Change the default user/%/edit to user/%/edit/profile.
  $useredit = &$items['user/%user_category/edit'];
  $userprofile = &$items['user/%user_category/edit/profile'];
  $useredit['page callback'] = $userprofile['page callback'];
  $useredit['page arguments'] = $userprofile['page arguments'];
  $useredit['access callback'] = $userprofile['access callback'];
  $useredit['access arguments'] = $userprofile['access arguments'];
  $useredit['file'] = $userprofile['file'];
  $useredit['file path'] = $userprofile['file path'];

  // Make the user/%/edit/profile sub-tab the default and left-most sub-tab.
  $userprofile['type'] = MENU_DEFAULT_LOCAL_TASK;
  $userprofile['weight'] = -10;
}

Change $userprofile = $items['user/%user_category/edit/profile'] to match the content profile path you need (in your case this would be $userprofile = $items['user/%user_category/edit/uprofile']

Also make sure your module runs after the content_profile module (your module must have a 'higher' weight than -1, which is content_profile.module's weight). Modules have a default weight of 0 so you should be fine there.

查看更多
登录 后发表回答