help, how to have a custom user profile path for each user role (rid)?

Super Druper - March 27, 2008 - 01:14

Hello,

alright Drupal experts, help me out with this one.

How can I have a unique user profile path for each role (rid), So, for example,if say my users are of role rid=4, I would like any link that points to view profile to point to domain.com/student/user-name and if they are of rid=5 to have it point to a user profile path of domain.com/teacher/user-name

Also, some modules I have installed that may have relevance to this question are: path auto, token and views, and I have clean urls enabled.

I've seen this done on some Drupal sites, so if anyone can point me in the right direction I would really appreciate it.

Thanks

subscribing, I was just

zywieco - March 28, 2008 - 08:55

subscribing,
I was just about to ask this myself

Got it

zywieco - March 28, 2008 - 10:03

it's configurable in admin/settings/pathauto
(I'm using the title field instead of username, check the replacement patterns)

Pattern for all student paths:
student/[title-raw]
Pattern for all /teacher paths:
teacher/[title-raw]
etc.

thanks for the pointer. yes,

Super Druper - March 29, 2008 - 22:58

thanks for the pointer. yes, in fact that's where i was looking but as far as I know there's only a single option for user path settings, which if used, will point *all* users to the corresponding path, irrespective of the user role (rid). What I'm seeking, on the other hand, is an option within User path settings to have patterns for user account page path based on their role id. Now, I don't know if this can be accomplished using pathauto alone at present, or whether there's some other option available, programatically, or through modules, or using a combination of these.

(I've got the latest release of pathauto (5.x-2.1))

Looking forward to more input on this from everyone.

Thanks

Using custom_url_rewrite

Super Druper - April 4, 2008 - 00:52

I was able to change all 'user/' paths to a custom path of 'student/' using the custom_url_function (placed inside settings.php).

<?php
function custom_url_rewrite($op, $result, $path) {
  global
$user;

  if (
$op == 'alias') {
   
// Change all outgoing from 'user/' paths to 'student/'.
   
if (preg_match('|^user(/.*)|', $path, $matches)) {
      return
'student'. $matches[1];
    }   
  }

  if (
$op == 'source') {
    
// Change all incoming 'user/' paths to 'student/'.
   
if (preg_match('|^student(/.*)|', $path, $matches)) {
      return
'user'. $matches[1];
    }  
  }

  return
$result;
}
?>

The function above accomplishes 50% of what I want, I'm just having a difficult time adapting it to my need, which is to check for the user's role as one of the conditions for changing the path.

I tried variations of the code above to include a condition to check for the user role, but it would simply default back to the standard 'user/' path.

Here's an example I tried to adapt for two roles, student (rid=6) and teacher (rid=7) :

<?php
function custom_url_rewrite($op, $result, $path) {
  global
$user;
  if (
$op == 'alias') {

    if (
preg_match('|^user(/.*)|', $path, $matches) && in_array('student', array_values($user->roles))) {
      return
'student'. $matches[1];
    }   
    if (
preg_match('|^user(/.*)|', $path, $matches) && in_array('teacher', array_values($user->roles))) {
      return
'teacher'. $matches[1];
    }   
   
  }
  if (
$op == 'source') {

    if (
preg_match('|^student(/.*)|', $path, $matches) && in_array('student', array_values($user->roles))) {
      return
'user'. $matches[1];
    }  

    if (
preg_match('|^teacher(/.*)|', $path, $matches) && in_array('teacher', array_values($user->roles))) {
      return
'user'. $matches[1];
    }
  }

  return
$result;
}
?>

I've tried variations of the above by substituting && in_array('student', array_values($user->roles)) with :
&& in_array('student', $user->roles)
&& $user->roles == 'student' 
&& $user->roles == 6

I would really appreciate it if someone with PHP knowledge could give me a hand fixing the code above so that it works for two roles i.e. student (rid=6) and teacher (rid=7) as I've outlined above.

Help por favor!!

inspiration

zywieco - April 4, 2008 - 09:32

IMHO in_array('student', $user->roles) is correct.
Check the code in this comment for inspiration:
http://drupal.org/node/122303#comment-280665, which is about displaying real names vs. user names.
'decide which profile to load based on role' might be interesting for url rewrite (in template.php, not in settings.php)

 
 

Drupal is a registered trademark of Dries Buytaert.