I hope it's pretty clear from my question title itself. I want to override autocomplete provided by module such as privatemessage to not suggest the current user(self) who is trying to send message. I tried doing some research about it but all I got was about overriding JS file.

I have used automcomplete with my custom form long ago & when I prepared the JSON, I removed current user from the list. But what to do if I have to do the same for some contributed module ?

  • Which version of Drupal are you using? – Елин Й. Sep 8 '13 at 7:56
  • Its D7, I have edited tags :) – RajeevK Sep 8 '13 at 12:11

This is the working code

  • Add this to your custom module
  • Replace MYMODULENAME with your custom module name
function MYMODULENAME_query_privatemsg_autocomplete_alter($query) {
  global $user;
  $query->condition('u.uid', $user->uid, '<>');
  • Doesn't work for me. Error "An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: localhost/alpha/messages/autocomplete StatusText: Internal Server Error ResponseText: – RajeevK Sep 8 '13 at 3:59
  • I have the same code setup working on my local.I am not able to reproduce the error on my side. I assume you have reverted the changes you made else where before trying this snippet and after implementing this hook you cleared the cache – arpitr Sep 8 '13 at 8:44
  • I think it's working now as I am not getting any ajax error and it suggest only other names :) – RajeevK Sep 10 '13 at 9:57

If you look at the code in Privatemsg module, the autocomplete callback function provides a hook hook_privatemsg_autocomplete_alter(). You can use this hook and unset the current logged in user from the $matches array.

I don't have Privatemsg module installed in any of my current projects, so I cannot test it, but the following code should do it:

function YOURMODULE_privatemsg_autocomplete_alter(&$matches, $names, $fragment) {
  global $user;
  • Doesn't work, it still populate global user. – RajeevK Sep 8 '13 at 4:00
  • @RajeevK is this hook ever called? What's in $matches in it? – Mołot Sep 8 '13 at 12:23
  • @Mołot - Yes its called & I got user_1 & user_10 stdClass object with all their info once I DPM it.. – RajeevK Sep 10 '13 at 2:13
  • 1
    @RajeevK are they indexed by their uids in array? And are you taking it by reference for sure? – Mołot Sep 10 '13 at 6:27
  • @Mołot - They are indexed separately by UID. What do you mean by reference here ? – RajeevK Sep 10 '13 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.