drupaldrupal-menu

Drupal - Set 'user/%/edit/uprofile' to default tab on 'user/%/edit'


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'?


Solution

  • 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.