Pull in profile field for Views page title
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;
?>