profile module with views (and usernode)

ultimike - March 13, 2007 - 19:01

I've got a tricky problem that I'm trying to work out and I'm not sure how to proceed.

I'm using the profile, usernode, and views modules. In the end, I'd like to create a view that shows all users with links to each's profile page.

I've set up a couple of profile fields: profile_firstname and profile_lastname. I'd like the view to show the user's name in the format:

profile_lastname, profile_firstname

plus, I'd like the whole thing to be a link to the user's profile page. It is important that I keep the profile_firstname and profile_lastname fields separate. Here's what I have so far in the views_profile.inc file:

//----- from profile_views_add_field
...snip...
if ($field->name == 'profile_lastname') {
        $help = t('FGBC Custom profile last, first name field.');
        $others = array(
          'sortable' => true,
          'handler' => array(
          'views_handler_field_profile_default' => t('normal text'),
          'views_handler_field_profile_link' => t('themed userlink'),
      ),
          'uid' => 'uid',
          'addlfields' => array('uid'),
        );
}
...snip...

function views_handler_field_profile_link($fieldinfo, $fielddata, $value, $data) {
  if ($value) {
    $obj->name = $value;
    $uidfield = $fielddata['tablename'] . "_"  . $fieldinfo['uid'];
    $obj->uid = $data->$uidfield;
    return theme('username', $obj);
  }
}

This code resides in the views_profile.inc file.

So far, the code works - but it only displays the profile_lastname. How can I get the profile_firstname in there? I've tried adding 'profile_firstname' to the "addlfields" array, but that leads to a malformed query. I took a look at the "query_handler" attribute, but I'm not really sure how to apply it in this case.

How can I add an additional profile field to the 'views_handler_field_profile_link'? This seems to be at the core of my problem.

Can anyone assist?

Thanks,
-mike

solved it

ultimike - March 13, 2007 - 20:37

Woo Hoo! I solved my own problem after a bit more searching on Drupal.org...

Here's how I did it:

1. I added 'query_handler' => 'views_query_handler_fgbc_fullname', to the field definition.

2. Here's the aforementioned query handler:

function views_query_handler_fgbc_fullname($field, $fieldinfo, &$query) {
  $query->add_field('value', 'profile_firstname', 'profile_firstname_value');
}

3. Once I added that, I was able to access the profile_firstnamae_value in my field handler:

function views_handler_field_profile_link($fieldinfo, $fielddata, $value, $data) {
  if ($value) {
  //$obj->name = $value;
    $obj->name = $value . ', ' . $data->profile_firstname_value;
    $uidfield = $fielddata['tablename'] . "_"  . $fieldinfo['uid'];
    $obj->uid = $data->$uidfield;
    return theme('username', $obj);
  }
}

-mike

Need more precision

dsnoeck - May 14, 2007 - 12:31

Hello Mike,

Thanks a lot for these information, this is exactly what I need. But, can you give me more precisions about your modifications. Where did you do add what changes ?

For example where did I put if ($field->name == 'profile_lastname') { ... in the profile_views_add_field() function ?

Thanks for you help.

----------------
Keep Open Spirit

good point

ultimike - May 14, 2007 - 14:24

dsnoeck,

Ah, yes, seems like I forgot to mention where I put all that code.

I embarassed to say, but I hacked the views/modules/views_profile.inc file. All the code listed above went into that file.

-mike

But, where in the file ?

dsnoeck - May 16, 2007 - 13:51

No, you already have explain which file to hack, but the point I don't find is where, in this file, did I have to add the modification. On which lines ?

Thanks a lot,
Damien
----------------
Keep Open Spirit

emailed

ultimike - May 16, 2007 - 15:21

I just emailed you the file...

-mike

This is exactly what I need, too

drupal777 - June 14, 2007 - 06:15

Is there any way you can be convinced to post the module here? Pretty please?

Mike, Thanks a lot for your

dsnoeck - May 24, 2007 - 07:51

Mike,

Thanks a lot for your code. Unfortunately I wasn't able to use it successfully ! But I have found an other way to have a quite similar result.

In the table view type, I have add 2 columns (Usernode: User ID & Usernode: Email). The first display the ID of the user and the 2nd display the email.
The code above will change this display by adding 2 smart images instead of the ID and the email. The first image is linked to the profile page of the user. The second image is linked to the email address.
The last function link the firstname to the user's profile.

I have found this code from the View Theme Wisard module. But you don't need to enable it ;-)


/**
* Function return an image linked to the profile page of the user, in the Phone list page.
*/
function phptemplate_views_handle_field_PhoneList_usernode_users_uid($fields, $field, $data) {
$info = $fields[$field['fullname']];
$path = 'user/'.$data->usernode_users_uid;

return l('', $path, NULL , NULL, NULL, FALSE, TRUE);
}

/**
* Function return an image linked to the email of the user, in the Phone list page.
*/
function phptemplate_views_handle_field_PhoneList_usernode_users_mail($fields, $field, $data) {
$path = 'mailto:'.$data->usernode_users_mail;

$info = $fields[$field['fullname']];
return l('', $path, NULL , NULL, NULL, FALSE, TRUE);
}

/**
* Function to link the firstname to the profile, in the Phone list page.
*/
function phptemplate_views_handle_field_PhoneList_profile_firstname_value($fields, $field, $data) {
$path = 'user/'.$data->usernode_users_uid;

$info = $fields[$field['fullname']];

if ($field['handler'] && function_exists($field['handler'])) {
return l($field['handler']($info, $field, $data->$field['queryname'], $data), $path);
}

if ($info['handler'] && is_string($info['handler']) && function_exists($info['handler'])) {
return l($info['handler']($info, $field, $data->$field['queryname'], $data), $path);
}

return l(check_plain($data->$field['queryname']), $path);
}

Cheers,
Damien
----------------
Keep Open Spirit

 
 

Drupal is a registered trademark of Dries Buytaert.