Hi All,

I am working on a module which among other things, creates a menu item containing several tabs. My question is whether or not it is possible to set the default tab on a per user basis? The idea is that the user can set which tab is the default through a simple admin interface.

I have already got the interface working, but I am not sure how to proceed with the setting of the default tab. Obviously I could use hook_menu_alter() to set the correct item to MENU_DEFAULT_LOCAL_TASK, but my concern is that this would change the default tab for all users. For example if User A was to set Tab 2 as the default, then Tab 2 would also become the default tab for all other users.

Given my limited knowledge of Drupal the only other option I can think of is to use caching and somehow create a cached version of the menu for each user. That way each user would get their own version of the menu containing the tabs.

Could someone tell me if this is the right line of thinking? According to the book Pro Drupal Development "menus are cached on a per-user, per-locale basis". Does this mean that what I have been discussing is already implemented in Drupal core?

I am not really sure how to start, so would appreciate any hints.

Regards,

Ben

Comments

graysadler’s picture

Why not make is simple: redirect the user to the appropriate page during the page load. I haven't tried this, it's just an idea but you could create a module that does this in a couple of ways.

1. During hook_init, check the args() and if the page matches www.example.com/foo with no args(1) then it would check the user's permissions or whatever and call a drupal_goto('foo/bar').

2. This is the way I would approach it, but I'm not sure of any negatives for doing it this way. Create a function that is called during hook_menu. It would be a simple function that is called for the menu item 'foo.' This menu item would also be the MENU_DEFAULT_LOCAL_TASK. The function would check the user's permissions and call drupal_goto if the user needs to be redirected to another page.

You would need two menu items that return the same page but have different callbacks. The MENU_DEFAULT_LOCAL_TASK menu item would need to call the function that checks the user's permissions, and the other function would simply return the page the user is requesting.

HTH

Lead Developer and Founder of StreamRiot.com

begun’s picture

Thanks for the suggestions. I decided to go with the hook_init() option. For the benefit of others this is what I did:

function namecards_init() {
  // Create a session variable storing user namecard settings.  Use of session variable is to reduce the need to query the database.
  if (!isset($_SESSION['namecards_user_namecard_settings'])) {
    global $user;
    $result = db_fetch_object(db_query('SELECT * FROM {user_namecard_settings} WHERE uid = %d', $user->uid));
    dpm($result);
    $_SESSION['namecards_user_namecard_settings'] = $result;
  }
  
  // Redirect the user to their default page
  $current_uri = check_plain(request_uri());  // Get the URI of the page currently being loaded
  if ($current_uri == '/namecards') {
    switch ($_SESSION['namecards_user_namecard_settings']->default_page) {
      case 0: // Browse
        // No need to redirect since 'browse' is the default tab for path 'namecards'. This is defined in the corresponding view 'namecards_browse_contacts' 
        break;
      case 1: // Organizations
        drupal_goto('namecards/organization_names');
        break;
     case 2: // Events
        drupal_goto('namecards/event_names');
        break;
    }
  }
}