Last updated September 5, 2009. Created by heather on October 1, 2008.
Edited by bekasu. Log in to edit this page.
About the snippet:
This snippet will help you bring in an argument to choose a user, and then display a profile field as the view's page title.
The set up
- Users on my site could fill in a profile field "fullname" which I wanted to use in the title of my view.
- Create a view with your filters as needed; make sure your test user has the appropriate field filled in.
- Under Add Argument- use the pull-down menu to select "User: UID is Author", click Add Argument button. Under 'Default' select "Display all values". Don't put anything in title field.
- In the "Argument Handling Code" text area add the code below:
The code
Do not add <?php ?> around the code in this text field:
$uid = $args[0];
if ($uid) {
$profile = db_fetch_object(db_query("SELECT value FROM {profile_values} v LEFT JOIN {profile_fields} f ON f.fid = v.fid WHERE f.name = 'profile_fullname' AND v.uid = '%d'", $uid));
if (!empty($profile->value)) {
$view->page_title = check_plain($profile->value);
}
}
return $args;Done!
Now you should see your 'fullname' profile field appearing as the view page title.
Change the profile field name as suits your situation!
PS- thanks to Stella for your help in sorting this out!
Drupal 6 + Views 2
For drupal 6 use in the php code validator field
<?php
$uid = $argument;
if ($uid) {
$profile = db_result(db_query("SELECT value FROM {profile_values} v LEFT JOIN {profile_fields} f ON f.fid = v.fid WHERE f.name = 'profile_fullname' AND v.uid = '%d'", $uid));
if (!empty($profile)) {
drupal_set_title(check_plain($profile)."'s Photos");
return TRUE;
}
}
return FALSE;
?>
Comments
The Drupal 6 + Views 2 code
The Drupal 6 + Views 2 code that is posted did NOT work for me.
Specifically this line
drupal_set_title(check_plain($profile)."'s Photos");To make this work, I changed the line to:
$handler->options['title'] = check_plain($profile."'s Photos");I would also recommend moving the
return TRUE;outside of this IF statement:if (!empty($profile)) {}, but keep it withinif ($uid) {}. I suggest this because you probably still want to return the results if the UID exists, but the profile_fullname field does not.As such, my complete and recommended updated code would be:
$uid = $argument;if($uid) {
$profile = db_result(db_query("SELECT value FROM {profile_values} v LEFT JOIN {profile_fields} f ON f.fid = v.fid WHERE f.name = 'profile_fullname' AND v.uid = '%d'", $uid));
if (!empty($profile)) {
$handler->options['title'] = check_plain($profile);
}
return true;
}
Drupal 7 + Views 2
This won't work for Drupal 7 + Views 2, as db_result has been removed. So instead of:
db_result(db_query("..."))You must use this code:
db_query("...")->fetchField()So, the full snippet:
<?php$uid = $argument;
if($uid) {
$profile = db_query("SELECT value FROM {profile_values} v LEFT JOIN {profile_fields} f ON f.fid = v.fid WHERE f.name = 'profile_fullname' AND v.uid = '%d'", $uid)->fetchField();
if (!empty($profile)) {
$handler->options['title'] = check_plain($profile); // yep, drupal_set_title won't work
}
return true;
}
?>