adding profile fields

BradM - January 9, 2008 - 07:18
Project:Advanced Forum
Version:6.x-1.0-alpha2
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I have a few profile fields that I manually added by making database calls within forum-thread.tpl.php. For example, I get the member's location by adding this:

$sql = "SELECT value FROM profile_values WHERE (uid = %d) AND (fid = 5)";
$result = db_query($sql,$node->uid);
$location = db_fetch_object($result);

I then display it in the node/comments by adding this:
<?php if ($location->value) { print check_plain($location->value); } ?>

Does this seem fine, or is there perhaps an easier/safer way to add any custom fields added to the profile, via the /admin/user/profile page?

Brad

#1

Michelle - January 16, 2008 - 04:16

I can't speak to the query since I haven't dealt with the core profile module, but I suggest not putting it in the .tpl file. It's better to put that sort of thing in _phptemplate_variables and create a variable to use in your .tpl file.

Michelle

#2

Michelle - January 23, 2008 - 15:06
Status:active» fixed

Nothing further since my answer... Marking fixed.

Michelle

#3

jaydub - January 25, 2008 - 06:21

I believe that user_stats module holds promise for helping to provide user stats/profile/etc information. The module is clean and simple and should be able to be dropped right into advanced_forum.

http://drupal.org/project/user_stats

#4

Michelle - January 25, 2008 - 13:47

I've already looked at that module and it duplicates a lot of what I already do.

Michelle

#5

Anonymous (not verified) - February 8, 2008 - 13:51
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#6

gareth_w - February 26, 2008 - 15:31

Is it possible to post an example of this, placed into the _phptemplate_variables function? Is this the same one that you add to the theme to get it to call advanced forum in the first place?

Many thanks,
Gareth

#7

Michelle - February 26, 2008 - 18:36
Status:closed» active (needs more info)

When replying to closed issues, please set them active again. If I didn't get these by email, I never would have seen this.

Yes, that's the same function where the advforum call is. That's the function that prepares variables for the templates. In that example, you'd set $vars['location'] = $location.

Michelle

#8

gareth_w - February 26, 2008 - 20:29

Again, thanks for the quick response (you must be sick of me by now) and sorry I didn't set this to active - I kind of figured the system did it automatically without giving it a thought.

However, I can't quite get this to work, and am sure something is adrift.

in template.php

   function _phptemplate_variables($hook, $variables = array()) {
  if (module_exists('advanced_forum')) {
    $variables = advanced_forum_addvars($hook, $variables);
      $sql = "SELECT value FROM profile_values WHERE (uid = %d) AND (fid = 6)";
      $result = db_query($sql,$node->uid);
      $location = db_fetch_object($result);
      $variables['location'] = $location;
   return $variables; 
}

Is set. I'm not sure that I'm setting the %d or $node-> uid corretly though - are these sent through by default, or globally available to the system? I have checked, and 6 is the correct fid, and is populated for at least 1 user.

In forum-thread.tpl.php I have:

     <?php if (isset($location)) { ?>
         <div class="author-points">
            <?php print t('Location:') . ' ' . $location ?>
        </div>
        <?php } ?>

Which was cannibalised from another variable. Something is working somehow as "Location:" is printed for all users (even those who have not entered a location), but nothing follows it; which leads me to believe the SQL is not returning what I think it is.

Do you have any further suggestions as to what could be coming adrift here?

Many thanks,
Gareth

#9

gareth_w - February 27, 2008 - 13:20

OK, I have a fix ~ish, but I can't explain it. Derived from knowing far too much perl and having a lucky guess.

When doing the above SQL query directly from the template, "Object" is produced. Mapping that Object to value gives the location. Note that this only works when doing the mapping in the forum-thread.tpl.php file; in the function _phptemplate_variables section of the template I cannot get it to return anything, suggesting that the uid is not being set properly.

My fix is

       <?php // BEGIN AUTHOR LOCATION DISPLAY ?>
            <div class="author-points">
            <?php
            $sql
= "SELECT value FROM profile_values WHERE (uid = %d) AND (fid = 6)";
           
$result = db_query($sql,$userid);
           
$location = db_fetch_object($result);
            print
t('Location:') . ' ' . $location->value;
           
?>

        </div>
        <?php // END AUTHOR LOCATION DISPLAY ?>

I'd still like to know the proper syntax to do this in the proper function, but for now it's working, and I'll take working over elegant and proper any day.

Gareth

#10

Michelle - February 27, 2008 - 13:38

Sorry, I hadn't looked that close at his SQL. You need to change $node->uid to $vars['node']->uid. You get $node in the .tpl when $vars['node'] is processed.

Michelle

#11

gareth_w - February 27, 2008 - 16:48

The code in my template.php file now reads

<?php

 
function _phptemplate_variables($hook, $variables = array()) {
  if (
module_exists('advanced_forum')) {
   
$variables = advanced_forum_addvars($hook, $variables);
     
$sql = "SELECT value FROM profile_values WHERE (uid = %d) AND (fid = 6)";
     
$result = db_query($sql,$variables['node']->uid);
     
$location = db_fetch_object($result);
     
$variables['location'] = $location;
   return
$variables

?>

Have I got this right?

The template uses $variables instead of $vars (is this confusing things?), but this still returns nothing :(

Thanks again,
Gareth

#12

Michelle - February 27, 2008 - 17:50

Well, I don't know that much about using SQL in Drupal so I'm just assuming his SQL works. Unless the OP comes back, I'm not sure there's anythign else I can do. This isn't an advforum issue...

Michelle

#13

Michelle - March 12, 2008 - 04:29
Status:active (needs more info)» fixed

I guess the OP never came back. At any rate, you can access the user's profile fields from the .tpl file through the variable I added a while back.

Michelle

#14

Anonymous (not verified) - March 26, 2008 - 04:31
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#15

BradM - March 26, 2008 - 17:44
Status:closed» active

Hi,

I've been out of the loop for a bit, but I just installed the latest alpha today. Michelle, you mentioned that "you can access the user's profile fields from the .tpl file through the variable I added a while back." I'm guessing you mean you can do this by adding $profile->fieldname within the advf-forum-user.tpl.php file?

Well I tried that but so far no success. Using the core profile module, I added a few fields, named "profile_location" and "profile_title" -- and I call them using this code in advf-forum-user.tpl.php:

<?php print $profile->profile_title; ?>
<?php print $profile->profile_location; ?>

However, nothing is returned. Any ideas?

Also, on a related note, in advanced_forum.module, around line 187, it states:

if ($accountid === 0) {
    return;
  }

However, I was getting buddy, email, etc. display in anonymous posts. So I had to change this to $accountid == 0.

Brad

#16

BradM - March 26, 2008 - 18:13
Version:5.x-1.x-dev» 5.x-1.0-alpha7

forgot to switch version info

#17

fe@drupal.org - April 3, 2008 - 14:02
Status:active» active (needs more info)

$profile - Profile object from core profile. Usage: $profile->fieldname

this doesn't work for me neither

#18

valdemar - April 4, 2008 - 13:01

That doesn't work for me either, but I figured it out by doing an array dump of the $profile array. You can access it like this:

<?php
print $profile["Personal Information"]["profile_name"]["value"];
?>

(Personal Information is the "profile group" of the field)

The documentation should be updated to say this.

#19

fe - April 4, 2008 - 15:21

once again, doesn't work for me

#20

ozon - April 22, 2008 - 23:51
Version:5.x-1.0-alpha7» 6.x-1.x-dev

First, sorry for my bad english ;).
The Code of Advanced Forum is very buggy.

I have the same Problem with profile fields.
But i have a Workaround.

Place the Code in "themes/advforum/advf-forum-user.tpl.php

<?php global $user;
$acc = user_load(array('uid' => $accountid));
?>

Then you can access the user Profile Fields with
<?php print $acc-><your_profile-field>;?>

It works for me.
I hope it helps.

#21

BradM - April 23, 2008 - 14:41
Version:6.x-1.x-dev» 5.x-1.0-alpha7
Status:active (needs more info)» patch (code needs review)

Hi ozon, that fix worked for me! Thanks for the help. :)

#22

Michelle - May 7, 2008 - 02:23
Status:patch (code needs review)» fixed

#23

Anonymous (not verified) - May 21, 2008 - 02:23
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#24

pilot7 - June 18, 2008 - 12:51
Version:5.x-1.0-alpha7» 5.x-1.0-alpha10
Status:closed» active

I have had this same problem, even through trying all of the following:
1) Enabled the Core profile package, added a new field called profile_name, and completed this value
2) Edited advf-forum-user.tpl.php and added Name:

<?php
print $profile->profile_name;
?>

At this point, all that displayed was Name:, the actual name did not display

3) I verified that the profile_name field had 'Public field, content shown on profile page but not used on member list pages.' and also tried it with, 'Public field, content shown on profile page and on member list pages.' In both cases I got the same result

4) Added the following code per a prior post to advf-forum-user.tpl.php file. Still did not display the actual name

<?php
global $user;
$acc = user_load(array('uid' => $accountid));
?>

Name:

<?php
print $acc->profile_name;
?>

I am sure I am missing something, but I cannot figure out what. Can anyone provide a pointer?

- Steve

#25

Michelle - August 10, 2008 - 02:47
Status:active» fixed

You don't need to load the user object as it's already loaded. Just use the existing $profile variable. It works but it wasn't documented correctly in the .tpl. I've updated it with the proper information.

$profile - Profile object from core profile. Usage: $profile['category']['field_name']['value']
  Example: Real name: <?php print $profile['Personal info']['profile_name']['value']; ?>

Michelle

#26

Anonymous (not verified) - August 24, 2008 - 02:55
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

#27

dazhdbog - August 26, 2008 - 22:04
Version:5.x-1.0-alpha10» 6.x-1.0-alpha2
Status:closed» active

still not working for me on alpha2.. dumps of both $profile and $account are giving NULL.

ozon's solution is returning profile fields of the _currently logged in user_ : (

#28

Michelle - September 3, 2008 - 15:19
Status:active» fixed

I've confirmed that this works in the current 6.x.

Michelle

#29

Anonymous (not verified) - September 17, 2008 - 15:22
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.