Realname for Privatemsg autocomplete
tborrome - February 4, 2009 - 22:08
| Project: | RealName |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
| Issue tags: | realname for privatemsg |
Description
Is there a way to make the Realname module substitute usernames displayed in Username Autocomplete fields? Like the one used in CCK's User Reference Fields and PrivateMessage Username fields (as shown in the attached screenshot)?
| Attachment | Size |
|---|---|
| username_autocomplete.JPG | 10.5 KB |

#1
I don't have PrivateMessage, so I can't say. I can intercept CCK UserReference. If you will send me a message with my Contact form, I will send you a replacement module because I need a better test than I can set up.
#2
Okay, I think I have Privatemsg working too.
#3
I'm also interested in realname appearing in the select list for a userreference field. Also in having it display the real name in the node view screen.
I have a little code here, but it's definitely not complete. It would be a lot easier, maybe, if userreference.module added support for realname. I won't count on it, though.
Here's some code, but I don't think I'm heading in the right direction. This code goes in realname_form_alter. The first case grouping goes in the switch statement. The next part goes outside the switch statement.
The names display correctly in the form, but when the form is submitted, an error is received because it doesn't like the real name as a user name.
case 'content_admin_display_overview_form':foreach ($form['fields'] as $field_name=>$field) {
if ($field['type']['#value'] == 'User Reference') {
$form['fields'][$field_name]['teaser']['format']['#options']['realname'] = 'Real name';
ksort($form['fields'][$field_name]['teaser']['format']['#options']);
$form['fields'][$field_name]['full']['format']['#options']['realname'] = 'Real name';
ksort($form['fields'][$field_name]['full']['format']['#options']);
}
}
break;
if (ereg("_node_form$", $form_id)) {foreach ($form as $key => $value) {
if (ereg("^field_", $key) && $value['uids']['#options']) {
foreach ($value['uids']['#options'] as $uid => $name) {
if ($uid != 'none') {
$account = user_load(array('uid' => $uid));
$form[$key]['uids']['#options'][$uid] = realname_make_name($account);
}
}
}
}
}
#4
Read #1 again. I have a working version, although I have thought of a way to make it a bit more generic. But I would dearly love some testing as is.
#5
The "Authored by" field on the node-edit form could use realname autocompletion as well. I've attached a patch that adds this feature for the 6.x version of RealName. While it isn't exactly what tborrome is looking for, the code I've written should make it easy to get the same functionality on other autocomplete fields.
#6
@prof1337: If you would like to see the code I currently have, contact me. There are two different autocomplete types that I have found: 1) by username, and 2) by uid. I am currently handling both in my code. But I think I need to generalize it a bit more to handle more cases than this issue asked for. One example of that is probably what you are pointing out here.
#7
I am awaiting the news about your testing.
#8
I responded to your email, but I'll post the response here as well:
"This is great! Thanks!
The thing it doesn't seem to do at this point is display the userreference realname in the normal node view.
Am I overlooking something?
It seems to me that the best way to do that is add a display option in the "Display fields" tab so that the site admin can decide whether to display the realname or the $account->name."
#9
I am not even going to think about making changes to a CCK module, especially in 5.x. The maintainer of that module would have to do that.
I will now look at cleaning this up and seeing if I can port it to 6.x.
#10
Fair enough. I also just noticed that the realname isn't appearing in the comment submission info. I'm looking through the code now to see what's going on. I'll let you know.
Thanks again!
#11
I added one line to the hook_comment:
$comment->name = isset($account->realname) ? $account->realname : $comment->name;
That seemed to do it.
/*** Implementation of hook_comment().
*/
function realname_comment(&$comment, $op) {
switch ($op) {
case 'view':
if (!$comment->homepage) {
$account = user_load(array('uid' => $comment->uid));
realname_make_name($account);
$comment->homepage = isset($account->homepage) ? $account->homepage : NULL;
}
$comment->name = isset($account->realname) ? $account->realname : $comment->name;
break;
}
return;
}
#12
Well, I see now that this functionality is enabled by clicking the "Override node theme" function. Unfortunately, I use the $node->name for other custom modules so I can't turn that on. Hmmm... Not sure what I'm going to do.
#13
Hi all,
NancyDru, if you could post your 5.x code here maybe I could try to port it to 6.x ?
#14
Here it is. It really needs to be more generic. The patch I did for the new table also had an autocomplete function that may be used in place of one of these.
#15
#16
Not sure if/how it's related, but see http://drupal.org/node/445404
#17
Thanks NancyDru and Amc !
FYI I'm working on this, but I have a sticky point with changing the callback function of Userreference Autopopulate. In D5 I can see how NancyDru is modifying the autocomplete function, by changing [#autocomplete_path] where it appears.
[field_user_reference] => Array
(
[#tree] => 1
[#type] => fieldset
[#description] =>
[0] => Array
(
[user_name] => Array
(
[#type] => textfield
[#title] => user_reference
[#autocomplete_path] => userreference/autocomplete/field_user_reference
[#default_value] =>
[#required] => 0
)
)
)
...
In D6 I tried to do the same thing. I can change userreference_autocomplete_value for a custom function and display the realname instead of the user login for fields that have NID values. But for the autocomplete, I can't find any [#autocomplete_path] in the form :
[field_user_reference] => Array
(
[#theme] => content_multiple_values
[#title] => Contact
[#required] => 0
[#description] => Pas trouvé ? Ajouter une nouvelle personne dans la nouvelle fenêtre et réessayer ici.
[0] => Array
(
[#type] => userreference_autocomplete
[#default_value] => Array
(
[uid] => 31
)
[#value_callback] => userreference_autocomplete_value
[_weight] => Array
(
[#type] => weight
[#delta] => 1
[#default_value] => 0
[#weight] => 100
)
[#title] =>
[#description] =>
[#required] =>
[#weight] => 0
[#delta] => 0
[#columns] => Array
(
[0] => uid
)
[#field_name] => field_user_reference
[#type_name] => testrealname
)
)
...
Somewhat the [#autocomplete_path] is hidden in the field [#type]=>userreference_autocomplete and the callback is defined in userreference_autocomplete_process(). I don't know CCK, fields, widgets and FAPI enough to find my way here, how do I just change the function called by the UserReference Autocomplete widget ?
Rest of the port is a easier, for example I can easily change the autocomplete function used by PrivateMsg because it's a textfield with a visible [#autocomplete_path].
Then there's still the reverse lookup problem like Berdir said, I'm going to think about it but I'm afraid of Black silence's remark : "real names are not unique, reverse lookup is near to impossible i guess". So maybe the best that could be done would "kind of" work... Or with a trick like displaying the RealName + another unique field like the user login or email... and doing the search only on the RealName. Just guessing, I don't know if that is even possible to do.
[EDIT] I have a working version of PrivateMsg/Realname/autocomplete, I'm still working on it and should post a proposal soon.
#18
This patch implements the autocomplete for the recipient of a PrivateMsg message. NancyDru, thanks for the new table realname, it's really usefull. For the autocomplete I didn't use your new realname_ajax_autocomplete_user() because :
1) We need to check if the user has allowed private messages. I know you're looking for a generic function however but how to handle specific cases like this one with a generic function ? Maybe realname_ajax_autocomplete_user() could detect the path and if it's messages/new, build another query...
2) I believe that the usability is best if the autocomplete does not depend on the order of the fields in the realname. For example, some may use for the realname : Lastname Firstname, others use Firstname Lastname. With
LIKE LOWER('%%%s%%')in the query instead of
LIKE LOWER('%s%%')the users can start typing either the first name OR the last name without any difference (or any profile field used in realname). I think it's very useful, because it's always nicer to display Firstname Lastname and it's always quicker to search with the lastname (they are less common). The inconvenient is that at the first letter entered, a lot of users that have that letter inside of their realname appear too, but with a few more letters the results should quickly be more acurate. Maybe it could be used in the generic function realname_ajax_autocomplete_user() too ?
3) I couldn't use your function because PrivateMsg needs the standard user name, the form doesn't validate with a realname. I believe it's the same for CCK Userreference and for a lot of other modules. How to handle this ? What if 2 users have the same realname ?
So I did the following : when we type the letters, the display is either :
- the standart user name
- the realname (and the standart user name in parenthesis)
However when an user is selected, the display becomes always the standart user name
- it's not a problem for the first group of users that don't have a different realname
- for users that have a different realname than their user name, the realname disappears, but it's not SO bad, because we still have in mind that it's associated with the realname we wanted to write to (or CCK reference and so on).
What do you think ? And sorry for writing so much !
For the CCK Userreference I'm still stuck at calling a custom autocomplete function, I'll ask the CCK folks.
#19
I've managed the CCK User Reference autocomplete to work with Realnames but it requires much more code than in D5 and I'm wondering if this should be included in Realname or in a new module, so I suggest to split the issue. For CCK User Reference autocomplete see #380460: CCK User Reference autocompletion integration with Realnames.
For Privatemsg maybe let's keep that issue. Here's a new patch which is the same as my previous one, except all the CCK stuff is cleaned. It's very basic and I won't work more on it since I don't need it at all (I just need the CCK).
#20
Thanks. Hopefully I can get to reviewing this soon.
#21
This is great. Thanks a lot for this precious feature! :)
I've tested it. For single user the autocomplete function works great.
I don't know if it's a generic autocomplete issue or of this module...
If I enter for example "max" -> the autocomplete suggests me "Max Mustermann" -> I take it. Then I click again into the autocomplete field to enter the next name but during typing a comma I receive an error -> you can see it in the attached image.
It works if I'm faster than the autocomplete function. But I think this is for the most users not really helpful.
EDIT: Sorry, forgot to mention that the posted error is in German. It means "An error has occured".
#22
See also:
http://drupal.org/node/468512
http://drupal.org/node/445404
#23
patch from #19, without the error message when writing to multiple recipients
#24
I've tested the patch from #23.
I always receive the same error message (see attachment) when the autocomplete function tries to validate the entered values.
#25
sorry :(
#26
Patch from #23 works great for me.
Thanks a lot! :)
#27
I can't get the patch to actually patch. I've used both cygwin and another patcher on windows to try to patch these files but each time i run the patcher it gives me the following errors (in screenshot attached). What am I doing wrong? I'm a bit of a noob to any sort of advanced Drupal development so it must be something i'm not doing right. Any help appreciated.
I'm using the patch from #25 and the most recent version of the realname module for Drupal 6.12 (not 6.13 yet).
Thanks
#28
patch was made for the -dev of realname. do you use the dev version or stable?
#29
Ah, that would explain a lot! I'm using the latest stable version of realname... So the next silly question is how stable is the dev version? Enough to test on a site that aims to be production-ready in the next 9 months?
By the way, thank you so much for the swift reply, you've no idea how much time you've saved me in trying other options like changing end-of-line formats, etc!
#30
I'm having another issue. The autocomplete feature works when you begin a new private message, but it uses usernames, not real names. What am I doing wrong?
#31
Bumping this as I still cant get this feature to work. I've tried using 25 and 23 patches but neither seem to work. I'm using cygwin to patch the realname.module file (-dev version), uninstalling the old module from my site, removing the files, copying over the new files, reactivating the new dev version of realname, then running update.php. It successfully updates, I configure realname, recalculate the names, re-index the site search, and still it only autocompletes on usernames. I really need this feature on my site but am not a PHP coder. Can anyone help with this? It seems i'm the only one who this isn't working for so what am I doing wrong?
#32
Just realised I had not added to this - I managed to get the patch working, it was me being an egit that was the problem and nothing to do with the patch. However, what would be great for this integration was if once the user types in the recipients name, the 'to' field displays that name, rather than reverting to the username. Having rooted around on here for a while I can see why this would be difficult, but the question is - is it possible or likely to be implemented?
#33
I too would like the realname displayed in the To box even if it uses the username behind the scenes.
I have modifed it to remove the username from the () but am a little stuck now.
This will also need to change the private message email message.... this might be better handled in private message. im going to request in there as well.
Phil
#34
Fixed by #579040: Some bugfixes.
#35
Included in 6.x-1.3