I am setting up a school site using Drupal 7
I have multiple user roles:

  • Student
  • Teacher
  • Parent
  • Administrator
  • etc.

I am using the Profile2 module to collect different information for each of them (But this could work using the core profile module as well). They all share the "main profile", as well as having a profile type for each role. I would like to be able to set a different RealName for the different roles. Ideally it would work something like this.

Main Profile collects:

  • Salutation (Mr.)
  • First Name (Brian)
  • Last Name (Lewis)

Name are displayed as follows:

  • Student RealName: B. Lewis (This would help with online privacy issues, but I don't know how to get just the first initial, it might have to be collected separately? Perhaps in the Student Profile.
  • Teacher RealName: Mr. Lewis
  • Parent RealName: Brian Lewis

Something else to think about is the fact that some of our teachers have students in the district, so they would have both roles (teacher and parent). In this case I think it would be appropriate for the teacher role to be weighted heavier than the parent role, so that the teacher version of the RealName is displayed.

I found an issue queue for the 6.x version over here, but since I'm working with 7.x I thought it was appropriate to open a separate issue.

Comments

geerlingguy’s picture

I've been working on this for a couple hours, and it looks like Realname doesn't allow for much dynamic changing of displayed user names. If I use core's theme_username, I can hook into template_preprocess_username() and do a little bit to the name depending on the user viewing the name, but if I use Realname, it looks like everything is just pulled straight from the database or cache, meaning I can't dynamically alter the realname.

I'd like to do this for privacy reasons as well; non-administrators on my site should only be able to see the first name, but not the last name (in my case).

Dave Reid’s picture

In response to the original request, we have an API hook for that:
hook_realname_pattern_alter(&$pattern, $account)

Dave Reid’s picture

Correct, once the real name is generated, it is saved to the database. I would suggest also implementing hook_username_alter() in a custom module to display usernames how you want for special roles like administrators.

geerlingguy’s picture

hook_username_alter() was just the ticket for me... but the hard thing is that I'm required to call for the $user->name instead of $user->realname (I could also call theme('username', $account))... kind of negates some of Realname's effectiveness :-/

Of course, this is a new hook in Drupal 7, so I might not need Realname for this particular site anymore, since I'm not really using any other features of Realname at this point.

bjlewis2’s picture

I don't want to sound needy, but I'm not at all a developer... Could I get some sample code to play with? or directions to somewhere I can learn more? right now, I'm using separate profiles for each role, then stringing them all together in the realname pattern box so that each of them will show up. This works as long as no user has multiple roles (profiles). However, I'm left with the following string:

[user:profile-tea:field-name-s] [user:profile-tea:field-name-l][user:profile-pri:field-name-s] [user:profile-pri:field-name-l][user:profile-sta:field-name-s] [user:profile-sta:field-name-l][user:profile-zpar:field-name-s] [user:profile-zpar:field-name-f]

Which isn't exactly ideal...

Dave Reid’s picture

Status: Active » Fixed

This is a good use case for hook_realname_pattern_alter(&$pattern, $account). You can provide a default pattern based on user account objects that have a profile2 profile. You'd have to do this in code.

fraweg’s picture

Hello,

I would like this feature too. Have someone did this and get it to work?

I Think this is nearly to my feature request "http://drupal.org/node/1541782". With this feature everyone can do "Different RealName for different roles and/or Profile2 profiles" without code something. Am I right?

Best regards
Frank

fraweg’s picture

..

Oceanman’s picture

A unique title for each role would be a very useful thing. Dave has given us some clues how to do this using the api in #2 and hook_username_alter() in a custom module in #3. I would like to just build this module and give it to you all but I am not sure how, yet.

Is anyone else here able to create a module? If anyone decides to start this module, please post here http://drupal.org/forum/4 so we can follow along and help test it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mike.roberts’s picture

How do we use the API? The functions are empty in the realname.api.php file and using mymodule_realname_pattern_alter(&$pattern, $account) or mymodule_realname_alter(&$realname, $account) seem to do nothing.

Grayside’s picture

Since this is a fairly high search result for simply using Profile2 with RealName, if you are trying to do something basic you need to turn on the Entity Token module, which ships with the Entity project. That will make profile tokens available on the RealName configuration screen.

functions’s picture

I know this is an old issue, but I had a similar need and was able to create a little module based off Dave Reid's suggestion. This is the entire contents of my little .module file:

The module checks if a user has a certain role, and if the user does, then I change the token pattern for that user's RealName:


/**
 * Implements hook_realname_pattern_alter().
 */
function listing_name_realname_pattern_alter(&$pattern, $account) {
  if(in_array('this special role', $account->roles)) { 
  // This statement checks to see the user has 'this special role'

    $pattern = '[user:field_special_role_name]';
    // you can find a list of all available tokens at example.com/admin/config/people/realname
    // or just create a new field in the user profile that contains the value you want assign as the RealName
  }
}

I installed the RealName module, but I left the default pattern for all users, as I only wanted to change the RealName if users had the role 'this special role'. If you needed unique RealName patterns for multiple roles, you could simply copy the if() statement and swap out 'this special role' for the other role you want to change the RealName pattern for (and obviously change the RealName token too).

Thanks @Dave Reid for pointing me in the right direction!

jbfelix’s picture

Hi i used your code but it does not work.
Could you share your module ?