I'm trying to print a field (in my case - the field 'field_state') from the 'uprofile' node, created by CP, into the author pane block.

this, for example, is working great :
echo $content_profile->get_variable('uprofile', 'title');

but this one isn't (giving 'Array'):
echo $content_profile->get_variable('uprofile', 'field_state');

Sorry if that's a really basic quastion, but I folowed your comments and the ones in the author pane module and I can't get this to work.
Thanks.

Comments

andreiashu’s picture

Hi tsi,
So in fact you are trying to print out the value of a CCK Field that is on a CP node type, right ?
(I really don't know how $content_profile->get_variable('uprofile', 'title'); works for you)

tsi’s picture

Yes, I guess, thought it should be simple enough.

andreiashu’s picture

Title: How to print a CP field into author pane ? » How to print a CCK field ?
tsi’s picture

Thanks, but this won't do any good unless you can direct me to what to do with that function.
Second, what about all the theming options metioned in the README file :

<?php
 // Just output the title of the content profile of type 'profile'
 // If there is no such profile, it will output nothing.
 echo $content_profile->get_variable('profile', 'title');

 // Get all variables of the content profile of type 'profile'
 $variables = $content_profile->get_variables('profile');
 
 // Print out a list of all available variables
 // If the user has no profile created yet, $variables will be FALSE.
 print_r($variables);

 if ($variables) {
   // Print the title and the content.
   echo $variables['title'];
   echo $variables['content'];
 }
 else {
   // No profile created yet.
 }
 
 // $content_profile also allows you to easily display the usual content profile's view
 // supporting the same parameters as node_view().
 echo $content_profile->get_view('profile');

?>

Isn't there somthing here that supposed to do what I am looking for ?
thanks

asak’s picture

The problem is you have nothing in $content_profile (I think)

Anyway, to print a cck field you should get the node in $node, possibly:

$node = content_profile_load('uprofile', $user->uid);

and then use you something like this:

print $node->field_state[0]['value'];

to print the field. notice you need $user as well.

Rosamunda’s picture

I´ve managed to make this work, but I have to put the code at the very ending of my node template, because it seems like after it loads the content profile, won´t show anything else from the original node.
Is there a way to "cut off" the loading of content_profile_load?

Thanks!!

andreiashu’s picture

Title: How to print a CCK field ? » How to print a CP field

I just read through the comments of Michelle and Fago in #316009: add content profile docs for themer and realise that this issue is in fact related to CP. So changing back the subject.

@asak: about #5: I think it is better to use content profile's method of loading data than CCK's. Also I have the slight feeling that your method in #5 could open up a security hole (see fago's comment #20 on #316009: add content profile docs for themer).

@tsi: when you do

$variables = $content_profile->get_variables('profile');
print_r($variables);

what does print_r($variables); output. The variable 'field_state' does not exist in $variables but we must find out why (probably you are looking for the wrong variable name ? ).

asak’s picture

@andreiashu - I tried the CP method with no success. about security - this needs to be enclosed in a check_plain():

print check_plain($node->field_state[0]['value']);
tsi’s picture

'field_state' is a cck field I'v created so you should replace it with whatever field you would like to print.
The code I'm using right now to make this work is :

<?php $node = content_profile_load('uprofile', $account_id); ?>
		<?php if (!empty($node->field_x)): ?>
    		<span class="author-pane-x">
				 <?php print check_plain($node->field_x[0]['value']); ?>
    		</span>
	<?php endif; ?>

*replace x with the field's name

YesCT’s picture

I think in order for the permissions per cck field to work, it needs to be run through node_view, not only check_plain...

so it would be something like print node_view(node_load($nid)); and print node_view(content_profile_load($uid));

... except I tried that and it did not work. (nothing gets printed after "print name").

I tried, logged in as user 1:

$new_leader_nodevalue = content_profile_load('profile', $leader_item['uid']);
      $new_leader_node = node_view($new_leader_nodevalue);
// print name
echo "print name ";
      if (0 != strcmp($new_leader_node->field_firstname[0]['view'],"")) {
        print $new_leader_node->field_firstname[0]['view']; }
andrenoronha’s picture

subscribing
thanks for this post :)

Bilmar’s picture

subscribing

uomeds’s picture

Subscribing. About to try #9.

uomeds’s picture

The code in #9 is working beautifully for me! Thank you.

One request - Does anyone know how to limit the number of characters from a field that are printed? For example, I am trying to print a date field that is only a year value (4 digits, eg. 2013). However, it is printing the full stored value as

2013-00-00T00:00:00

----------------------
edit: Never mind. Figured it out.

<?php $node = content_profile_load('uprofile', $account_id); ?>
<?php if (!empty($node->field_class)): ?>
    <span class="author-pane-class">
<?php print substr(check_plain($node->field_class[0]['value']), 0, 4); ?>
    </span>
<?php endif; ?>
andrenoronha’s picture

You may try this more elegant way:

<?php print date('Y', strtotime($node->field_class[0]['value'])) ?>

Y for year
m for month
d for day

etc.

Bilmar’s picture

you can refer to http://php.net/manual/en/function.date.php for different formatting options to use with #15

uomeds’s picture

Thanks a.luiz.n and trupal218. I tried that method and it works nicely but seems to be subtracting one year from each date. Is there any way I can make it display Y+1 to correct for this?

Also, I am now trying to take it one step further and replace my default 'user picture' field in my author panes with a custom uprofile filefield imagefield (field_profile_picture).

I need the following statement:

<?php $node = content_profile_load('uprofile', $account_id); ?>
  <?php if (!empty($node->field_profile_picture)): ?>


  <?php endif; ?>

to define the following variables:

1) $image_filepath = stored url for the picture in that field
2) $alt = "(username)'s picture"
3) $title = "(username)'s picture"
4) $attributes = unknown, blank I think

So that I can print the new uprofile pictures, with any given 'preset_name', via the following standard imagecache string:

<div class="picture">
  <a href="/users/(author-pane-user-name)" title="View user profile.">

     <?php   print theme('imagecache', 'preset_name', $image_filepath, $alt, $title, $attributes);    ?>

  </a>
</div>

I feel this would be an awesome way to let uprofile take over one more core responsibility and make the 'user picture' far more flexible in how in can be implemented.

I know it might be getting tricky, but any help with calling and defining those variables would be great.

Thanks.

andrenoronha’s picture

uomeds,
let me see If I got what you want...
You want to print the imagecache-preseted user picture which was uploaded with a fileField in a content profile?

I made my own tricky way to do this, tell me if that's what you want....

uomeds’s picture

Yes, a.luiz.n, that is exactly correct. The field is a filefield type imagefield, provided via uprofile. I want it printed using a specified imagecache preset.

Any way to do this would be great. It's the last function my site needs.

Thanks.

andrenoronha’s picture

Ok.

I used this node: http://drupal.org/node/432854 as reference, but their solution didnt work for me...
That's what I did:

1. I found and implemented this good function that returns a string between strings that I pass as parameters:

function get_string_between($string, $start, $end){ 
  $string = " ".$string;
  $ini = strpos($string,$start);
  if ($ini == 0) return "";
  $ini += strlen($start);
  $len = strpos($string,$end,$ini) - $ini;
  return substr($string,$ini,$len);
}

2. I overrode this function to return the picture:

function phptemplate_user_picture($uid) {
  if ($uid != 0) {
    $profile = content_profile_load('YOUR_PROFILE_NAME', (int) $uid);
	
    if ($profile->field_NAME_OF_YOUR_FIELD[0]['filepath']) {
      $dir = 'avatar/';
      $imgpath = $profile->field_NAME_OF_YOUR_FIELD[0]['filepath'] . '!'; //add ! at the end so that I can use it as the end parameter in get_string_between
      $imgname = get_string_between($imgpath, 'PIC_PATH/', '!');
    }
    else {
      $dir = 'imagefield_default_images/';
      $imgname = 'THE_NAME_OF_YOUR_DEFAULT_IMAGE.png';
    }

    global $base_url;

    return '<img src="'. $base_url .'/sites/default/files/imagecache/YOUR_PRESET/' . $dir . $imgname . '" alt="' . $profile->title . '" />';
  }
}

and Voilà

I hope it helps you... At least hope you understand my code...
if not, try to figure out how the path to the imagecached user picture is.

uomeds’s picture

I tried adding the following to my advanced_profile_author-pane.tpl.php:

function phptemplate_user_picture($uid) {
  if ($uid != 0) {
    $profile = content_profile_load('uprofile', (int) $uid);

    if ($profile->field_profile_picture[0]['filepath']) {
      $dir = 'avatar/';
      $imgpath = $profile->field_profile_picture[0]['filepath'] . '!'; //add ! at the end so that I can use it as the end parameter in get_string_between
      $imgname = get_string_between($imgpath, '/sites/default/files/profile_picture/', '!');
    }
    else {
      $dir = 'imagefield_default_images/';
      $imgname = 'THE_NAME_OF_YOUR_DEFAULT_IMAGE.png';
    }

    global $base_url;

    return '<img src="'. $base_url .'/sites/default/files/imagecache/authorpane_profile/' . $dir . $imgname . '" alt="' . $profile->title . '" />';
  }
} 

But nothing happened. The page just loads no different with or without it. I didn't enter a proper default because I don't intend to have a default.

I've checked and both listed directories exist and are correct:

/sites/default/files/profile_picture/
/sites/default/files/imagecache/authorpane_profile/

However there is only a "demo image" in ../imagecache/authorpane_profile/, so imagecache is obviously for some reason not being triggered to activate by the above code.

Any ideas?

uomeds’s picture

Never mind. I got it working via:

http://drupal.org/node/470004#comment-1626220

HongPong’s picture

I found this when looking for how to print filepath CCK filefields. Like so, if field_episode is a filefield:

print $node->field_episode[0]['filepath']

ressa’s picture

Version: 6.x-1.0-beta3 » 6.x-1.0-beta4

I needed to populate the "Default value" field of a User reference (Customer unit zone) with a User reference(field_partner_reference) from the current user's Content profile ('customer_unit').

A user can create several Customer unit zones, and in the end both Content profile ('customer_unit') and Customer unit zones point to the same partner. I hope it kinda makes sense... 8o)

So I needed to refer the new Customer unit zone to the person ('partner') responsible for the Customer automatically, this did the trick:

global $user;
if($user->uid!=1){
  $node = content_profile_load('customer_unit', $user->uid);
  return array(  0 => array('uid' => $node->field_partner_reference[0]['uid']) );
}
else return array();
tevih’s picture

Hello,

I must be pretty thick, as I can't get this to work! I'm trying to print a content profile cck field in the author pane, as the OP. I confirmed the name of the field by doing this:

 $variables = $content_profile->get_variables('profile');
print_r($variables); 

Which let me know that the field is field_profile_company. So I used the following code as in #9 to print it, in the author pane template file:

<?php $node = content_profile_load('uprofile', $account_id); ?>
<?php if (!empty($node->field_profile_company)): ?>
     <h2>Company</h2>
     <span><?php print check_plain($node->field_profile_company[0]['value']); ?>    </span>
<?php endif; ?>

But it didn't display anything. And yes, there is data for that field.

What am I doing wrong?

tevih’s picture

Ugh!! I spent 5 hours trying to figure out how to theme the author pane, and less than 5 minutes after posting in frustration, I figure it out!!

I changed:
$node = content_profile_load('uprofile', $account_id);

to:
$node = content_profile_load('profile', $account_id);

I see here that other people are using 'uprofile' in their code - why the difference?

Håvard’s picture

If you need ACTING USER'S full name you can use global $user->uid instead of $account_id:

global $user;
$profile = content_profile_load('uprofile', $user->uid);
if (!empty($profile->field_firstname) || !empty($profile->field_lastname)) {
$fullname = $profile->field_firstname[0]['value'] . " " . $profile->field_lastname[0]['value'];
print $fullname;
}

If you need AUTHOR'S full name you can do this:

$profile = node_load(array('type'=>'uprofile', 'uid'=>$object->uid));
if (!empty($profile->field_firstname) || !empty($profile->field_lastname)) {
$fullname = $profile->field_firstname[0]['value'] . " " . $profile->field_lastname[0]['value'];
print $fullname;
}
Michelle’s picture

@tevih: It's the name of the node type so it depends on what you called your node type.

Michelle

tevih’s picture

Thanks Michelle - that makes sense.

guntherdevisch’s picture

Hi all,

I have a custom content type named 'CV', which is also a Content Profile field.

I'm trying to print out the value in my user-profile.tpl, but it doesn't work. Can someone help? My Content Type is a file field. I want to show the value + make it clickable to the users.

I allready tried using the following:

$cvtest = content_profile_load('cv', $account_id);
print $cvtest->field_cv[0]['value'];
print $cvtest->field_cv[0]['content'];
print $cvtest->field_cv[0]['file'];
print $cvtest->field_cv[0]['view'];
print check_plain($node->field_cv[0]['file']);

Thanks in advance!
Gunther

mashizhao’s picture

#9 works like a charm! Thanks for sharing!

Priyanka Singh’s picture

#9 works perfectly..thanks @tsi...

sjiuh’s picture

How i got it to work

opdrachtgever_profiel = node type
field_abonnement_vorm = desired field

$abbo = ($content_profile->get_variable('opdrachtgever_profiel', 'field_abonnement_vorm'));
print($abbo[0]["value"]);

Hope it helps someone,
Maarten

KA FileMaker Pro developers