Hello. I'd like my posted articles to display the author's full name. I am using
story mode in Drupal 4.6.,

I have enabled the Profile module and have setup first name and last name for
the users posting articles. In node.tpl.php for my theme, I tried invoking
$profile_lastname and $profile_firstname (the names of the variables in
the profle) and it does not work. The line comes up empty. $name is working,
and $submitted, of course, is working.

Anyone knows how I can do this?

Many thanks.

-S

Comments

Phillip Mc’s picture

you need to add in an extra line to "initialise" or load up the profile.module details..i.e. profile_load_profile($account)

Where $account = the $user->uid of the current user.

I haven't tried this..but, it should be as simple as this:

<?php profile_load_profile($user->uid); ?>
<?php print $user->profile_fullname; ?>

Where $user->uid is the current user and profile_fullname is the name of the profile field that stores the users full name.

Phil

slazenger’s picture

Many thanks for the quick tip.

I tested this and it didn't print anything on the front page where the node teasers are all listed. I made sure I was not goofing up on the variable names itself. I don't if there is a way to debug this kind of thing -- this is my first dip into Drupal code blocks.

I can't tell whether $user->profile_lastname etc will also be visible to the program from anywhere?

-S

merlinofchaos’s picture

The problem is most likely that $user isn't filled in in your theme. If you're doing it in node.tpl.php, try using $node->uid instead.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

slazenger’s picture

The function definitions in in profile.module seem to expect
a particular kind of variable to be called to load the values it
looks like. I did test this code putting into node.tpl.php and it didn't work either -- it doesn't particularly surprise me.

profile_load_profile($node->uid);
print $node->profile_lastname;

I think the issue is that the custom field names like profile_lastname and profile_firstname that I created are themselves variables. Which means it is not clear that merely referring to them as $user->profle_lastname will work or for that matter $node->profle_lastname.

I will need to look into the exact way profile.module is written to see how to extricate the profile_XX values I want. However there ought to be standard hook to do this too.

It's not resolved yet, so any inputs will help. I'll post back my final resolution, needless to say.

-S

merlinofchaos’s picture

Hum. From my perusing of the code, this should work. Try this for your debugging pleasure:


$user->uid = $node->uid;
profile_load_profile($user->uid);
print "&quot; . var_export($user, true) . &quot;";

That should give you a good idea exactly what is being loaded by profile_load_profile.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

gdinh’s picture

Try putting $user->profile_lastname in curly brackets.
I have run into the same problem as you and solved it with this:

<?php profile_load_profile($user);  ?>
<?php print "{$user->profile_lastname}"; ?>

I'm not sure why the curly brackets are needed here, maybe some guru will venture an explanation?

alexis’s picture

The curly braces are needed in PHP for objects attributes and array variables between double quotes, I couldn't find the explanation about objects but the array function manual page explains the usage for arrays.

Alternatively you could forget about the double quotes and curly braces and just do:

print $user->profile_lastname;

Regards!

Alexis Bellido - Ventanazul web solutions

slazenger’s picture

First off, thanks to both Merlin and Giao. I managed to get it to work with your inputs and some more poking around. The profile.module functions are referring to these fields as $user->{$field->name} for e.g.

Here's the clip (in node.tpl.php) that worked starting from the if ($submitted) check:

if ($submitted):

 $user->uid = $node->uid; 
                         profile_load_profile($user); 
                         print $user->{profile_firstname}; 
                         print $user->{profile_lastname}; 
                        - print $date

endif;

Appreciate your inputs.

-S

munga’s picture

In this way, the full name will be used everywhere instead of
the username.

Put this function in template.php

function phptemplate_username($object) {
  if ($object->in_preview) {
      return theme_username($object);
  }
  return _phptemplate_callback('username', array('object' => $object));
}

and this is the content of username.tpl.php (that maybe can
be shorter). This is not my code :)

if ($object->uid && $object->name) {
  // If the user has a full name defined, use that
  $user = user_load(array(uid => $object->uid));
  if (!empty($user->profile_fullname)) {
    $object->name = $user->profile_fullname;
  }

  // Shorten the name when it is too long or it will break many tables.
  if (drupal_strlen($object->name) > 20) {
    $name = drupal_substr($object->name, 0, 15) .'...';
  }
  else {
    $name = $object->name;
  }

  if (user_access('access user profiles')) {
    $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));

  }
  else {
    $output = $name;
  }
}
else if ($object->name) {
  // Sometimes modules display content composed by people who are
  // not registered members of the site (e.g. mailing list or news
  // aggregator modules). This clause enables modules to display
  // the true author of the content.
  if ($object->homepage) {
    $output = '<a href="/'. $object->homepage .'">'. $object->name .'</a>';
  }
  else {
    $output = $object->name;
  }

  $output .= ' ('. t('not verified') .')';
}
else {
  $output = variable_get('anonymous', 'Anonymous');
}

print $output;

comments ?

burch003’s picture

I tried munga's other way of doing it and it didn't work.

I would prefer to replace every instance of the username with a field I have in profiles called profile_full_name. I've seen several threads that talk about it, but none that come up with a way to do it that works.

Does anyone know how? Am I missing a thread that explains it?

merlinofchaos’s picture

munga's method should work in 4.7.

In 4.6 it's pretty difficult to make that replacement. Not sure which you're running.

-- Merlin

[Point the finger: Assign Blame!]
[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

-- Merlin

[Read my writing: ehalseymiles.com]
[Read my Coding blog: Angry Donuts]

burch003’s picture

The field name on my site is 'profile_full_name', not 'profile_fullname', which is why it wasn't working. Silly me.

Thank you for this contribution.

-burcham

Rik van der Weij’s picture

Where do I find those files on a clean drupal installation? I only see them in the cck directory. And that didnt seem to work.

jphelan’s picture

i used method above and it works, but i can't post comments now, when i do i get this message:
"You have to specify a valid author."

korayal’s picture

i am getting the same error too, i can't post comments if this module is enabled, since Drupal 5.1 is installed.

jphelan’s picture

So after playing around a bit I got this to work in 4.7 and don't get the "You have to specify a valid author." error. Basically it just disables it on the comment pages, pretty simple. Also I have my profile's set up for married couples so my code test to see if the individual is single or married and then creates the profile name accordingly. Hope this helps.

<?php
if ($object->uid && $object->name && (arg(0) != "comment")) {
  // If the user has a full name defined, use that
  $user = user_load(array(uid => $object->uid));
  if (!empty($user->profile_name1)) {
    $object->name = $user->profile_name1;
  }
    if (!empty($user->profile_name2)) {
    $object->name .= " & ";
    $object->name .= $user->profile_name2;
  }
    if (!empty($user->profile_lname)) {
    $object->name .= " ";
    $object->name .= $user->profile_lname;
  }
  // Shorten the name when it is too long or it will break many tables.
  if (drupal_strlen($object->name) > 45) {
    $name = drupal_substr($object->name, 0, 15) .'...';
  }
  else {
    $name = $object->name;
  }

  if (user_access('access user profiles')) {
    $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));

  }
  else {
    $output = $name;
  }
}
else if (($object->name) && (arg(0) == "comment")) {
  // Disables code on comment pages so comments will post
  if ($object->homepage) {
    $output = '<a href="/'. $object->homepage .'">'. $object->name .'</a>';
  }
  else {
    $output = $object->name;
  }

} else if ($object->name) {
  // Sometimes modules display content composed by people who are
  // not registered members of the site (e.g. mailing list or news
  // aggregator modules). This clause enables modules to display
  // the true author of the content.
  if ($object->homepage) {
    $output = '<a href="/'. $object->homepage .'">'. $object->name .'</a>';
  }
  else {
    $output = $object->name;
  }

  $output .= ' ('. t('not verified') .')';
}
else {
  $output = variable_get('anonymous', 'Anonymous');
} 

print $output;
?>
steingard’s picture

This works quite well in Drupal 5.x, except for in the 'edit' form area.

I'm using CCK user reference module, and I'd love for this to also affect the select lists that are in the edit view of my node(s).

I suppose I have to inject this code somewhere into the CCK userreference.module somewhere...? :s I'm not too keen on that.

Any thoughts?

steingard’s picture

After attending the recent DrupalCampToronto, I was introduced to the use of Bio module with Auto Nodetitle module to produce user profiles that are nodes.

I then realized that I could use this in my Drupal site by using the CCK Node reference module and have have the user's Bio nodes filtered using a view that I setup in Views.

It doesn't exactly change username references on postings and everything, but it works nicely for CCK user referencing.

rb7’s picture

The root of the problem that leads to the "you have to specify a valid author" error is that the comment module expects $user->name to be a login name as defined in the user table. The code proposed above actually changes the value of $user->name for the user object passed to the theme function and so the comment module can't find a user with matching name value in the database. jphelan's proposed hack is to leave the original name unchanged on comment pages. This isn't necessary if you use a new variable for the full name rather than change the original object. My proposed modification to the function is below. Let me know if I've overlooked something, but it works for me. (My version of the function also presents the name as plain text rather than a link if the name shown is the current user.)

function phptemplate_username($userToTheme) {
  if ($userToTheme->uid && $userToTheme->name) {
    // Change name from login name to display name
    $thisUser = user_load(array('uid'=>$userToTheme->uid));
    $fullName = $thisUser->profile_firstName.' '.$thisUser->profile_familyName;
    // Shorten the name when more than 20 chars so it doesn't make tables too large
    if (drupal_strlen($fullName) > 20) {
      $name = drupal_substr($fullName, 0, 15) .'...';
    }
    else {
      $name = $fullName;
    }
    // Check if the name being displayed is the current user
    global $user;
    $nameIsUser = ($userToTheme->uid == $user->uid) ? TRUE : FALSE;
    // Link to profile page only if current user has access and
    // is not the same person as the name being displayed
    if (user_access('access user profiles') && !$nameIsUser) {
      $output = l($name, 'user/'. $userToTheme->uid, array('title' => t("View @user's profile", array('@user' => $thisUser->profile_firstName))));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($userToTheme->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($userToTheme->homepage) {
      $output = l($userToTheme->name, $userToTheme->homepage);
    }
    else {
      $output = check_plain($userToTheme->name);
    }
    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', t('Anonymous'));
  }
  return $output;
}
kriskhaira’s picture

this worked for me. thanks.

Evgenij’s picture

I would like that the users of my homepage can show their advertisement in the Blog entries. I provided a field in the user profile named “user_google_ads”. If a new user registers itself on my homepage, it can enter its Google AdSense number.

Google AdSende code for node.tpl.php in the theme:

if ($node->type == 'blog'){ if ($is_front) {} else {

if ($submitted): 
 $user->uid = $node->uid;
      profile_load_profile($user);
      print '
<div class="google">
<script type="text/javascript"><!--
google_ad_client = "'.$user->{user_google_ads}.'";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
google_ad_channel = "";
google_ui_features = "rc:6";
//-->
</script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>'; 
endif; 

} }

Kimeros’s picture

I'm guessing that this is slightly different from what you're wanting but it could be an option to just leave the top Submitted by Name as it is and put full info at the bottom of the page. Or even remove the submitted by line entirely and replace with this.

In 4.7, After you've activated the profile module and created your additional fields you just need to do go into the block section and enable the Author Information block.

I configured mine to display the block in the content and it puts a nice "About the Author" section at the bottom of my posts with my custom fields showing (You can select which of these are displayed)

venkat-rk’s picture

The new Authorship module is probably the easiest way to do this. Check it out:
http://drupal.org/node/67741

jphelan’s picture

The authorship module is great, but not for what I want to do. I have a church site with a forum and acidfree photo albums and want to use the member's real names as opposed to their username in the forums and comments, because their username is not always identifiable. The authorship module will not do this, but munga's code does it perfectly with one significant problem, I can't post any comments when it is working. I get this message: "You have to specify a valid author." See above for solution.

seth97’s picture

I agree, munga's code works great! Excellent to have the full name displayed instead of e.g. “mad_max55”!

There is one problem: the alias is still displayed in the user profile in the upper left corner (beside “view”). This means that whenever a user is posting something the full name is displayed, but if you click on his user profile his alias is shown at the top.....

Any ideas how to change the alias for the full name in the user profile?

Thanks!

jphelan’s picture

Seth97 check here for how to customize your profile pages http://drupal.org/node/16011

seth97’s picture

I am using the override for the user profile that you mentioned, it is great! But I can not control the upper part of the page where the alias is shown beside “view” and “edit” (I am running 5.1, with the standard theme garland).

Thanks!
/Seth

Walterh’s picture

I want to apply this proposed code in Drupal 5.1, but where can i find the file username.tpl.php ? or do I have to create this file first?

-----------------------------------------------
http://www.offstage.nl

ashtonium’s picture

Yes, username.tpl.php is a custom theme file. You will need to create it and place it in your theme's folder (same location as the template.php file you're editing).

Also, there is a handbook page on this topic, I've added a comment to it which should show you how to change your profile page titles: http://drupal.org/node/64248#comment-247442

scottrigby’s picture

ashtonium, the code in your comment worked really well for title on the profile page!

BUT I'm still trying to find a way to simply print the user's full name within my own custom content type.

Here's what I did:
- I made a new tpl page (in this case, node-coupon.tpl.php)
- I commented out :

  <!--<div class="content">
    <?php print $content ?>
  </div>-->

and broke out the details like this:

  <div class="coupon_title">
    <?php print $title; ?>
  </div>

  <div class="vendor_name">
    <strong>Restaurant Name:</strong> <?php print $field_vendor_name[0][view]; ?>
  </div>

  <div class="coupon_body">
    <?php print $node->content['body']['#value']; ?>
  </div>

etc.

The problem comes in when I try to print the customer's name (name of current user) on the coupon.

Here's the code I'm using now, based on what I gleaned from this thread, and your comment:

  <div class="customer_name">
    <strong>My Name:</strong> <!--<?php print $profile_fullname[0][view]; ?>-->
<?php
global $user;
profile_load_profile($user->uid);
print $user->name;
?>

And I added this code to the end of my template.php file:

/**
 * Create an alias instead of user username: http://drupal.org/node/64248
 */
function phptemplate_username($object) {
  if ($object->in_preview) {
      return theme_username($object);
  }
  return _phptemplate_callback('username', array('object' => $object));
}

/**
* An implementation of theme_user_profile.
*
* From: http://drupal.org/node/64248#comment-247442
*/
function phptemplate_user_profile($account, $fields) {
  // change the profile page's title to the specified profile field
  // but only if we are actualy viewing a profile page and if the specified profile field has a value
  if ((arg(0) == 'user') && $account->profile_fullname) {
    drupal_set_title($account->profile_fullname);
  }
 
  $output = '<div class="profile">';
  $output .= theme('user_picture', $account);
  foreach ($fields as $category => $items) {
    if (strlen($category) > 0) {
      $output .= '<h2 class="title">'. $category .'</h2>';
    }
    $output .= '<dl>';
    foreach ($items as $item) {
      if (isset($item['title'])) {
        $output .= '<dt class="'. $item['class'] .'">'. $item['title'] .'</dt>';
      }
      $output .= '<dd class="'. $item['class'] .'">'. $item['value'] .'</dd>';
    }
    $output .= '</dl>';
  }
  $output .= '</div>';

  return $output;
} 

And I made a new file called username.tpl.php, which includes munga's code above (because that was suggested in the post you responded to).

By the way, the full name DOES show up in "Submitted by", but I can not see the code to call THE CURRENT USER from my custom node.tpl.php file?

I'm guessing I'm just missing one tiny step somewhere?

Thanks in advance for any help

Scott Rigby
http://basekamp.com
http://PlausibleArtworlds.org

JulieLA’s picture

AHA! That's exactly what I was looking for. Thank you!

jayakrishnan-1’s picture

Having very strange problem here. I created a page with the above code and when i access the page , the user gets logged out

Here is the code

global $user;

$user->uid = $node->uid;
print "Name".$user->name; 

Using drupaal 5.1

jayakrishnan-1’s picture

Solved by changing the code to

global $user;
profile_load_profile($user->uid);
print "Name".$user->name; 
sylvie_n’s picture

Hi there,
I am creating profiles using node profile.
i have fields set up for first name and last name as 'content_type_first_name' and 'content_type_last_name' which are related to my profile node, (which is a child of the parent usernode).
I have found forums discussing how to override username throughout your site with the real name of the user, when using the profile module. Does someone know how to override the username when using nodeprofile instead of the profile module?
help much appreciated,
sylvie

davemybes’s picture

Take a look at this post for help with that: http://drupal.org/node/157072
______________________________________________________________________________________________________
mybesinformatik.com - Drupal website development

______________________________________________________________________________________________________

Steel Rat’s picture

Maybe I'm not sure what you're tying to achieve here, but the above code shows the CURRENT user on all nodes, not the node owner, comment owner, etc.

Steel Rat
My Drupal Sites:
RPGMapShare.com
Infinite Ordnance
Malvern Rouge Valley Youth Center

scottrigby’s picture

Thanks this helped solve the same problem I was having (users got logged out -> but in my case they were also re-logged in as the creator of the node - yikes).

BUT still can't get the fullname to show. It only shows the username - any ideas?

Scott Rigby
http://basekamp.com
http://PlausibleArtworlds.org