Hi!

Is it possible to use content_profile variables in a block?

E.g. I have a block about author of viewing node(blog) on my site. (his/her name, imagefield photo, some other content profile fields)

When I want to display data from author content profile in this block, I always do this way:
I take node id from argumens:

if (arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2))

then I load this node

$node = node_load(arg(1));

then I create sql for load of author profile node

$sql = "SELECT n.nid, n.uid FROM node n WHERE (n.type = 'uprofile' AND $node->uid = n.uid)";
$result = db_query($sql);
$row = db_fetch_object($result);
$node_loaded = node_load($row->nid);

and then finally I can use author profile fields of in a block

$pohlavie = content_format('field_profil_pohlavie_2', $node_loaded->field_profil_pohlavie_2[0], 'default', $node_loaded);

It works for me, however, the question is:
Is there some way how to replace all this steps and to use $content_profile->get_variable in this block?

Theoretically, I can use template file for this block and add it for content_profile variables, but still - I don't how to tell to function $content_profile->get_variable, which account/profile has to be used - in this example it is node author account.

Thanks for your opinion and explanation.
Igorik
http://www.somvprahe.sk

Comments

asak’s picture

Nice of you to attach the code you use to get it working, i was messing around with this as well...

I'm assuming that if adding the content_profile variable to the block theme then it would display the current users profile, but not sure why that's my assumption...

buckley’s picture

Hi,

Thanks for that piece of code

Can the developer make api function call for this? I think there is some fancy lazy loading involvde in the stander way of accessing the data.

Thanks!

benone’s picture

igorik: why you cant just use node variables ? What more $content_profile gives you ?

What about that in block body with php filter:

  $node = node_load(arg(1));
  $node_profile = content_profile_load(profile, $node->uid);
  if ($node_profile) {
    echo 'About me: '.$node_profile->field_about[0][value];
  }
igorik’s picture

Hi Benone,
good point.

I replace my code with yours and it works like a charm.
I didn't know about function content_profile_load before.

thanks
Igor

daneyuleb’s picture

While content_profile_load would work...if the block is on every page, isn't that a lot of overhead and mysql calls?

That content_profile var, already being set, would probably be preferable. If it was just available in the scope of a block.

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;
}
Håvard’s picture

**double post** please delete me..

anggao’s picture

subscribe

fooltak’s picture

Thank for #3. It works perfect on node page.
How can I load it on blog page by user?
I appreciate any suggestion.

fooltak’s picture

I could solve it by putting this.

if ((arg(0) == 'node') && is_numeric(arg(1))) {
$args[0] = arg(1);
$node_id = $args[0];
$node = node_load($node_id);
$user_id = $node->uid;
$node_profile = content_profile_load(profile, $node->uid);
} else if ((arg(0) == 'blog') && is_numeric(arg(1))) {
$args[0] = arg(1);
$user_id = $args[0];
$node_profile = content_profile_load(profile, $user_id);
} 
jimajamma’s picture

I needed this earlier and was just going to ask/post if this is a good way to do it, so, to continue the discussion, this is what I did in my module:

In the beginning of MY_MODULE.module, I put:

function MY_MODULE_init() {
  module_load_include('inc', 'content_profile', 'content_profile.theme_vars');
}

and then in my MY_MODULE_block() where I do my block view:

        case 'welcome':
          global $user;
          
          $cptv = new content_profile_theme_variables($user->uid);
          $foo=$cptv->get_variable('profile','field_avatar');
          $avatar = $foo[0]['filepath']; 

          // rest of code follows here to do with the filepath what you want, etc...

where I grab, in this case, my cck avatar field that is the user's picture. I know but I didn't feel like using $picture for all sorts of reasons :)
This example uses the uid of the logged in user, but if $node is available, you could use $node->uid instead I would imagine.

Comments? From what I can see, this should even cache things nicely if I am reading the README correctly...

Jim

mobaar’s picture

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

Sorry to resurrect, but I'm having my own issues on this.

I've created 2 variables, one in the standard Profile module, the other as part of Content Profile module.

Profile module: Variable is named fullname
Content Profile module: Variable is named field_profile_fullname

I'm trying to use phptemplate_username to change the way the display works so it will display the posting user's full name, preferably from the Content Profile module (field_profile_fullname). I haven't been able to get it to work.

Works 100% for the Profile module's "fullname" variable using the following code:

	if (!empty($user->fullname)) {
      if (drupal_strlen($user->fullname) > 20) {
        $name = drupal_substr($user->fullname, 0, 15) .'...';
      }
      else {
        $name = $user->fullname;
      }
    }

However, as mentioned earlier, it's preferable to use the field_profile_fullname variable found in Content Profile module. Here's the problematic code:

	$node_profile = content_profile_load('profile', $object->uid);
	if (!empty($node_profile->field_profile_fullname[0]['value'])) {
      if (drupal_strlen($node_profile->field_profile_fullname[0]['value']) > 20) {
        $name = drupal_substr($node_profile->field_profile_fullname[0]['value'], 0, 15) .'...';
      }
      else {
        $name = $node_profile->field_profile_fullname[0]['value'];
      }
    }

The issue, which I've tested, is that $node_profile->field_profile_fullname[0]['value'] is empty, and thus this code is not executed. I have a feeling it's just my lack of knowledge on the proper referencing. I've double-checked -- the variable name IS correct (field_profile_fullname) and the user I'm testing it on does have a value.

Any help?

Thanks!

~Mo