0

I have a set of users on the site I'm building that have First and Last name fields in their profile. How can I create a display that will show the user link as "Last name value, First name value" all inline?

There are a number of places where this is how I want user references to show up. In some other places, it would be ideal to show those fields, and a couple of others along with the user's image. Then of course there is the full profile view. I had though this must be done with different display types, but can not see anyway to bring fields inline with each other, or adjust what text makes up the link.

5

3 Answers 3

1

Another option is to use hook_username_alter

then in your template.php file located in /sites/all/themes/yourtheme

add the following code:

function yourtheme_username_alter(&$name, $account) {
  if (isset($account->uid)) {
    $this_user = user_load($account->uid);//loads the custom fields
    $name = $this_user->field_first_name['und'][0]['value'].' '.$this_user->field_last_name['und'][0]['value'];
  }
}

replace yourtheme with your actual theme name.

You might not want to alter the user name when you're in the user profile page. So you could do something like this:

function yourtheme_username_alter(&$name, $account) {
if (    arg(0) != 'user' // test if you are on user page
) {
  if (isset($account->uid)) {
    $this_user = user_load($account->uid);//loads the custom fields
    $name = $this_user->field_firstname['und'][0]['value'].' '.$this_user->field_lastname['und'][0]['value'];
       }
     }
}

replace yourtheme with your actual theme name.

2
  • 1
    That is what I had read that I was trying to remember! When I'm not stuck in a meeting I'll try that out!
    – UltraBob
    Feb 18, 2016 at 5:08
  • @UltraBob how did it go?
    – No Sssweat
    Feb 19, 2016 at 7:16
1

One option would be to use:

Real Name

The RealName module allows the administrator to choose fields from the user profile that will be used to add a "real name" element (method) to a user object. Hook_user is used to automatically add this to any user object that is loaded. It will also optionally set all nodes and comments to show this name.

For Example:

enter image description here

By default only the raw username is being shown, as you can see at the top of the page.

enter image description here

Change this to:

enter image description here

Result:

enter image description here

source: Change Drupal Usernames with the Real Name Module

0

Your question is very similar to Combine two fields to be displayed together where you'll find several ideas, some of which are solving your problem even without custom code.

Hope will help

5
  • Thanks, I hadn't found that, but I need these coming out as links to the user profile, and I need them to show up in a lot of different places, including as the rendered preview in the paragraphs module.
    – UltraBob
    Feb 17, 2016 at 10:34
  • And I never need the link to be the user name. I'd be totally fine with editing some kind of user.tpl.php template file to do this, if there is a way to make the change for just one display type. Is display suite really the best answer for that?
    – UltraBob
    Feb 17, 2016 at 10:37
  • @UltraBob, I guess you should give a try to Display Suite. Once done, you'll know how good it can support your work.
    – Augusto
    Feb 17, 2016 at 10:49
  • the thing about that is Display Suite is gigantic. I am trying to keep things slim, and I find it hard to believe that the best way to make user links show up in a compact form that uses some fields from the profile is to install a do-everything module.
    – UltraBob
    Feb 18, 2016 at 0:01
  • With that said, I am going to install it and have a look. Maybe it will solve some of my other outstanding issues.
    – UltraBob
    Feb 18, 2016 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.