I am using the Privatemsg module in Drupal 7. I have a hook in a custom module that is triggered when users write a message that is too short. I set this up using the instructions here.

Basically, it looks like this:

function mymodule_privatemsg_message_validate($message) {
  $uid = $vars['message']->author->uid;

  //if root or help user, message should always go through regardless of length
  if ($uid !== '1') {
    if ($uid !== '3') {

      if (empty($message->thread_id) && strlen($message->body) < 200 ) {
    form_set_error('[body][value]', t('My custom message.'));

I recently noticed there are not so many messages being sent on my site. I would like to know if it is because they are being blocked by this hook. How can I track how many times it is triggered (and even better if I can know which logged-in users are getting the error message)?

  • 4
    As side note, you are using $vars, but that variable has not been initialized from the function, nor is a function's parameter. – kiamlaluno Nov 8 '12 at 7:59
  • @kiamlaluno Sorry, I don't understand your comment. Could you please clarify here? drupal.stackexchange.com/questions/50144/… – Patrick Kenny Nov 9 '12 at 17:26
  • I answered, and I also said what else is wrong in your code. – kiamlaluno Nov 9 '12 at 18:26

I don't think you'll get 'statistics' as such without some coding yourself, I've never seen a module that does that sort of thing (but hey it's Drupal so there probably is one somewhere).

Failing that I think your best bet would be to use the the watchdog() function, combined with enabling the dblog module on your site. You can use it like so:

if (empty($message->thread_id) && strlen($message->body) < 200 ) {
  form_set_error('[body][value]', t('My custom message.'));

  global $user;
  watchdog('mymodule', 'My custom message for user @username', array('@username' => $user->name));

Every time that if statement is entered you'll get a message on the 'Recent log messages' page, where you can even filter by entries for your own module, giving you a basic idea of what's happening, when, and for whom.

  • Is it possible to e-mail myself the watchdog message? Or to send an e-mail with the same message? I don't need an elaborate solution; just some notifications I can't miss (e-mails) would suffice. – Patrick Kenny Nov 9 '12 at 3:26
  • @Patrick I don't think there's anything built in but the Logging and Alerts module should do the trick; one of its features is Allows routing of watchdog messages to various email addresses, based on their severity levels. – Clive Nov 9 '12 at 8:55

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.