0

I have done this by altering the 'page callback' for the path and then checking for the user role in the callback function. I am not sure if this is the best way to go about it.

function mymodule_hook_alter(&$items) {
  // change callback for user view page to our dashboard function
  $items['user/%user']['page callback'] = '_client_dashboard';
}

/**
 * Callback function for menu item user/%
 */
function _client_dashboard($account) {
  global $user;
  // since we override user.module page callback for user/%user 
  // perform default function for users without client role
  if(!in_array('client', $user->roles)) {
    // return the function used by user.module passing in account
    return user_view_page($account);
  }
  return t('Dashboard');
}

I know you cannot have logic in hook_menu_alter since it's only called when caches cleared and stored in routing table.

3
  • Do you need to have a completely different page for based on the user role?
    – awm
    Commented Jun 11, 2014 at 14:32
  • The user/%user page is redundant for my use case so I had planned to remove the menu item but since it's affect would be global and user module being core I thought is reuse it for my client role as the client dashboard and keep it's default functionality for other roles.
    – davewilly
    Commented Jun 11, 2014 at 20:51
  • @gerzenstl , how would you make it configurable? My first thought would be to add a new permission 'access client dashboard' or something, and do a user_access check for that instead of testing for a role? Would that be a good solution? Thanks
    – davewilly
    Commented Jun 11, 2014 at 20:55

1 Answer 1

1

The approach that you took is fine. It's one of the ways that you can control which page you have to show depending on (this case) the role.

Tip: take in consideration if "client" is going to be always the role allowed to see that page. If that could change in the future (add more roles or change to other), try to make it configurable.

0

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.