Using Nodeprofile fields elsewhere

Events happening in the community are now at Drupal community events on www.drupal.org.
jockox3's picture

I'd like to use this nifty little piece of code - http://drupal.org/node/64248 - to make all references to users show up as their "real name" rather than their username. But their "real name" in this case is made up of concatenating five fields from the user's nodeprofile content type.

Is it possible to refer to the nodeprofile's fields in this way? If so, how?

In the following code:

<?php
if ($object->uid && $object->name) {
 
// If the user has a full name defined, use that
 
$account = user_load(array(uid => $object->uid));
 
$profilename = $object->name;
  if (!empty(
$account->profile_fullname)) {
   
$profilename = $account->profile_fullname;
  }
 
// Shorten the name when it is too long or it will break many tables.
 
if (drupal_strlen($profilename) > 20) {
   
$name = drupal_substr($profilename, 0, 15) .'...';
  }
  else {
   
$name = $profilename;
  }
  if (
user_access('access user profiles')) {
   
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
  }
  else {
   
$output = check_plain($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 = l($object->name, $object->homepage, array('title' => t('View link')));
  }
  else {
   
$output = check_plain($object->name);
  }
 
$output .= ' ('. t('not verified') .')';
}
else {
 
$output = variable_get('anonymous', 'Anonymous');
}
print
$output;
?>

I tried changing

  if (!empty($account->profile_fullname)) {
    $profilename = $account->profile_fullname;
  }

to

  if (!empty($account->content_type_site_member->field_forename[0]['view'])) {
    $profilename = $account->content_type_site_member->field_forename[0]['view'];
  }

but it didn't do anything. So I'm a bit stuck. Any ideas?

TIA,

Jock

Comments

I am also interested in this....

seth97's picture

I have also used the override that you mentioned. It worked great when I used the Profile module, but it would be good to also have it with Profiles as nodes.

/Seth

some ways you could attack this

firebus's picture

afaik, fields from node profile content types are not included in the user object. perhaps they should be! this might make for a rather unwieldy user object but i guess that's the case for complex standard profiles as well.

there are a couple of ways to attack this that spring to mind.

#

the first would be the horrible solution of making direct database queries for the information and using that.

SELECT field_forename FROM {content_type} WHERE uid = $account->uid

however, depending on your set up you might have to handle the case where the user has not filled out some or all of these fields yet, and you'd have to handle running check_plain or whatever formatting on the content of the field

#

a cleaner solution would be to write a CCK widget that implements hook_user and places the contents of it's fields into the user object (or maybe it would make more sense to use hook_profile_whatever in this case?)

#

maybe the right solution would be to add some functionality to node profile to (optionally? per content type? per field?) include node profile data in the user object!

just load the nodeprofile

fago's picture

just load the nodeprofile node
$node = nodeprofile_load('your_profile_type', $object->uid);

and make use of the data as desired..

nodeprofile doesn't automatic load the nodeprofile into the user object.

Stupid me

jimdunntx's picture

But fago, where should this code be loaded. Yes, a bit new at this.

Actually, my problem is that I am trying to access nodeprofile fields (cck fields) in relation to usernode in views. More specifically, I am trying to bring up an imagefield off of a filter on usernode

A little help please

bfbryan1811's picture

Fago,
I am trying to load the nodeprofile data onto my user_profile.tpl.php page and am having some trouble.

Here is what I have to load the data.

<?php
$info
= nodeprofile_load('profile', $object->uid);
?>

and this is what I am using to display the data.

<?php
print $info->field_last_name_value;
?>

It doesn't seem to be working though.

Split database

bfbryan1811's picture

Fago,
The issue seems to be that when I set up multiple node profiles (for diff roles) and use the same field across both types the database splits and sets up field_first_name as a seperate table as opposed to keeping it in both types profile1 and profile2. Is that supposed to happen? And is that what is causing the lack of data loading?

thanks

Is there data in $info? you

criz's picture

Is there data in $info? you can use print_r($info); to check...

Then I think displaying the data should look something like: print $info->field_last_name_value[0]['view'];

cheers,
criz

nodeprofile_load() is in -dev version

thepanz's picture

I was wandering why in my nodeprofile.module there wasn't any nodeprofile_load() function.

I'm using 5.x-1.1 version, and checking the 5.x-1.x-dev version I found that function.

Hope that helps someone! :)
Bye

-thePanz-

Also, you could change your

rconstantine's picture

Also, you could change your name from five fields to one cck_fullname field. However, if you've got five fields, I would imagine you have prefixes and suffixes which I don't currently have in my module. That is a good idea though. Check out the cck_fullname module.

Fullname instead of Username

seth97's picture

Hi!
Did anyone actually get this to work using Profiles as nodes? It worked excellent with the profile module using: http://drupal.org/node/64248

I am using the excellent tutorial for profiles as nodes: http://shellmultimedia.com/tutorials/user-profiles
and would very much like to have the Fullname displayed instead of the Username everywhere on my site!

Thanks!
/Seth

This works for me!

seth97's picture

This solution was provided to me by www.hyrme.com!

My user profile are base on Michelles tutorial, ver. 2: www.shellmultimedia.com/tutorials/user-profiles

Include the following code in your template.php file.

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

Insert this code in a new file and name it 'username.tpl.php' and place it in your theme directory. You need to have the full name as a CCK field in the content type 'uprofile' and name the field: 'field_full_name'. If you have other name you need to change the two rows:

<?php
  $account
= node_load(array('type'=>'uprofile', 'uid'=>$object->uid));
 
$fullname = $account->field_full_name[0]['value'];
?>

username.tpl.php:

<?php
if ($object->uid && $object->name) {
 
$profilename = $object->name;
 
// If the user has a full name defined, use that

  //Change the following two lines to load your fullname from wherever
  //it is stored and put it to $fullname..
 
$account = node_load(array('type'=>'uprofile', 'uid'=>$object->uid));
 
$fullname = $account->field_full_name[0]['value'];
 
//Change - end of your modifications. $fullname should contain the name..

 
if (!empty($fullname)) {
    
$profilename = $fullname;
  }
 
// Shorten the name when it is too long or it will break many tables.
 
if (drupal_strlen($profilename) > 20) {
    
$name = drupal_substr($profilename, 0, 15) .'...';
  }
  else {
    
$name = $profilename;
  }
  if (
user_access('access user profiles')) {
    
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
  }
  else {
   
$output = check_plain($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 = l($object->name, $object->homepage, array('title' => t('View link')));
  }
  else {
   
$output = check_plain($object->name);
  }
 
$output .= ' ('. t('not verified') .')';
}
else {
 
$output = variable_get('anonymous', 'Anonymous');
}

//remove the "test" string below - this is to make sure that this code
//is kicking in as needed.
//print "test: " . $output;
print $output;
?>

bug

dugh's picture

This doesn't work if you try to add a comment somewhere while logged in.
It says that you need a valid username.

Profiles as nodes

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week