Hi,

I have succesfully created some fields for my users' profiles. I would like to be able to show some of these each time they post a comment on the site (e.g. their location).

How do I do this? My theme is based on phptemplate.

I have tried putting in the name of the field in the comment.tpl.php, e.g. $profile_location but it returns nothing.

Any help appreciated.

Comments

cre8d’s picture

Sorry I didn't mean to post this twice.

dublin drupaller’s picture

Hi Cre8d,

I don't have a definitive answer for you...but since nobody else picked up on this, I thought I would chip in.

As I understand it there are only a certain amount of variables available via the phptemplate engine for the comment.tpl.php file.

So (I stand corrected) I think you have to insert a few lines into your phptemplate file where the variables are set for comment.tpl.php and add in a $user_location variable, for example, that references the corresponding profile field you have setup.

It's a very very good question....and I for one, would love to see an example of how to do this for you're specific needs. Phptemplate is superb, but, for newbies like me who are not proficient in php it's a bit of a struggle to get to grips with how things are done.

if you haven't come across this already, there are handbook pages for Making additional variables available to your templates, but there are no simple examples for newbies...

Would be keen to see an example of how you/someone else has done this.

Dub

DUBLIN DRUPALLER
___________________________________________________
A drupal user by chance and a dubliner by sheer luck.
Using Drupal to help build Artist & Band web communities.

Currently in Switzerland working as an Application Developer with UBS Investment Bank...using Drupal 7 and lots of swiss chocolate

Adagio’s picture

This is a snippet to lay the ground for showing some profile fields in comments in a phptemplate theme. Paste it into the top of comment.tpl.php in your theme. That might be the one in the phptemplate directory. Then write the code that shows the profile fields you want as outlined below.

You can get a formatted profile field by calling

profile_view_field($commentator, $field['profile_fieldname']);

The title from admin->settings->profile is available in

$field['profile_fieldname']->title

By using the title member variable you only have to update in admin -> settings -> profile to have the change reflected here. The variable $have_profile will tell you whether the current comment has a registered user associated with it and one or more profile fields are succesfully loaded. The $commentator variable contains the user object and the $fields array contains all fields defined in admin->settings->profile.

Example:

if ($have_profile) {
  // enclose in div. For instance, this could be left aligned
  // right before the $content is shown
  print '<div>';

  // Get the formatted fields to show
  $birthday = profile_view_field($commentator, $fields['profile_birthday']);
  $homepage = profile_view_field($commentator, $fields['profile_homepage']);

  // Show birthday if present
  if ($birthday) {
    print '<b>'.$fields['profile_birthday']->title.':</b> ';
    print $birthday;
    print '<br />'; // a break if it fits your theme
  }

  // Show example homepage link. We'll use our own title here
  if ($homepage) {
    print '<b>Commentators homepage:</b> ';
    print $homepage;
  }

  print '</div>';
}

Note that the code will refuse to show private fields by default.

It should be noted this chunk of code does use some resources as it loads the commentators user object on each comment rendering, and a better approach performancewise would be to change comment.module. Also, I suppose many don't like this mixture of logic and presentation. Personally, I don't think it's such a big deal here.

Here is the code to paste on top of comment.tpl.php

if ($GLOBALS['profile_hack_initialized'] !== true) {
    // Initialization of hack
    // For performance reasons we get the field definitions here, as it
    // is not needed for every comment.
    // @see profile_view_profile
    $GLOBALS['profile_hack_fields'] = array();
    if (module_exist('profile')) {
      $result = db_query("SELECT * FROM {profile_fields} WHERE visibility != %d", PROFILE_PRIVATE);
      // put each field as an object into the $fields array
      while ($field = db_fetch_object($result)) {
          $GLOBALS['profile_hack_fields'][$field->name] = $field;
      }
    }
    $GLOBALS['profile_hack_initialized'] = true;
}
$fields =& $GLOBALS['profile_hack_fields'];

// If there are field definitions and a user associated with the comment, set
// a flag that profile fields are ready to be shown. It is determined by
// checking if the commentators user object and some field definitions is
// succesfully loaded.
$have_profile = ((count($fields) > 0) and ($commentator = user_load(array('uid' => $comment->uid)))) ? true :  false;

Final note: Updated the code, use current version please.

Adagio’s picture

This one automates the process a bit at the expense of flexibility.

if ($have_profile) {
  // Setup an array with visible fields. Change this, and see the
  // change reflected in the output.
  $visible_fields = array(
    'profile_birthday',
    'profile_homepage',
    'profile_location'
  );
  // Go through the $visible_fields array and show each one.
  foreach ($visible_fields as $visible_field) {
    if ($formatted_field = profile_view_field($commentator, $fields[$visible_field])) {
      echo '<b>'.$fields[visible_field]->title.':</b> ';
      echo $formatted_field;
      echo '<br />';
    }
  }
}

If you want all fields filled out by the user outputted in this manner, use this code:

if ($have_profile) {
  foreach ($fields as $field) {
    if ($formatted_field = profile_view_field($commentator, $field)) {
      echo '<b>'.$field->title.':</b> ';
      echo $formatted_field;
      echo '<br />';
    }
  }
}
cre8d’s picture

Thanks everyone - I will give it a shot now.

maui1’s picture

Hi Adagio,

Thought you might be able to help with this related snippet request.

I am trying to list profile names and maybe photo fields ordered by the contents of a list field (professions) filled out by the members.

So that I would get an alphabetical list of Professions with members names and photos listed according to the profession they selected.

Any pointers would be greatly appreciated.

coupet’s picture

Excellent contribs!

Apache is bandwidth limited, PHP is CPU limited, and MySQL is memory limited.