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 |
Jump to:
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
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
Nothing further since my answer... Marking fixed.
Michelle
#3
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
I've already looked at that module and it duplicates a lot of what I already do.
Michelle
#5
Automatically closed -- issue fixed for two weeks with no activity.
#6
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
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
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
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
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
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
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
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
Automatically closed -- issue fixed for two weeks with no activity.
#15
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
forgot to switch version info
#17
$profile - Profile object from core profile. Usage: $profile->fieldname
this doesn't work for me neither
#18
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:
<?phpprint $profile["Personal Information"]["profile_name"]["value"];
?>
(Personal Information is the "profile group" of the field)
The documentation should be updated to say this.
#19
once again, doesn't work for me
#20
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
Hi ozon, that fix worked for me! Thanks for the help. :)
#22
#23
Automatically closed -- issue fixed for two weeks with no activity.
#24
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:
<?phpprint $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
<?phpglobal $user;
$acc = user_load(array('uid' => $accountid));
?>
Name:
<?phpprint $acc->profile_name;
?>
I am sure I am missing something, but I cannot figure out what. Can anyone provide a pointer?
- Steve
#25
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
Automatically closed -- issue fixed for two weeks with no activity.
#27
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
I've confirmed that this works in the current 6.x.
Michelle
#29
Automatically closed -- issue fixed for two weeks with no activity.