0

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 ?

0

2 Answers 2

1

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, '<>');
}
3
  • 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
    Commented Sep 8, 2013 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
    Commented Sep 8, 2013 at 8:44
  • I think it's working now as I am not getting any ajax error and it suggest only other names :)
    – RajeevK
    Commented Sep 10, 2013 at 9:57
2

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;
  unset($matches[$user->uid]);
}
9
  • Doesn't work, it still populate global user.
    – RajeevK
    Commented Sep 8, 2013 at 4:00
  • @RajeevK is this hook ever called? What's in $matches in it?
    – Mołot
    Commented Sep 8, 2013 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
    Commented Sep 10, 2013 at 2:13
  • 1
    @RajeevK are they indexed by their uids in array? And are you taking it by reference for sure?
    – Mołot
    Commented Sep 10, 2013 at 6:27
  • @Mołot - They are indexed separately by UID. What do you mean by reference here ?
    – RajeevK
    Commented Sep 10, 2013 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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