4

I'm using the Token Authentication and Privatemsg modules together. When a user receives an e-mail notification that they he/she has received a private message, I have placed a link (using a token provided by the Tokenauth module in a query string) that allows the user to log in and see the message.

At the bottom of the page is the message reply form. I would like the user to be able to reply via this form as well (without logging in, via token authentication). However, when I tested this, I received the following error:

The form has become outdated. Copy any unsaved work in the form below and then reload this page.

A Google search revealed this helpful comment on drupal.org:

So it seems you would have to either

unset($form['#token']) or $form['#token'] = NULL

Privatemsg has two message creation forms, the new message form and the reply to message form.

To fix the new message form, I added unset($form['#token']); to hook_form_privatemsg_new_alter() (new messages) and hook_form_privatemsg_form_reply_alter() (replies). I tested this and new private messages as well as replies can now be sent.

I then disabled caching for the privatemsg module so that the messages would always be refreshed.

However, I have a rule that is set to Show a message on the site ("Your message was sent to User X!") whenever a private message or reply is sent; this rule is not triggered when a private message is sent if the user is authenticated via Tokenauth even though the private message is saved to the database properly and displayed upon page refresh.

How can I make sure rules are still triggered when authenticated via Tokenauth?

2
+100

From my point of view, it would seem Rules are not being triggered at all. You could, however, try invoking a rule manually by placing a rules_invoke_event() call within inside a custom form submit function.

Note that the #submit property on Drupal forms allow multiple submit handlers to be called for a single form.

In the hook_form_privatemsg_new_alter() and hook_form_privatemsg_form_reply_alter() form alters you have implemented you should be able to find and modify the #submit property. Just do something like:

// Implementation of hook_form_FORMID_alter().
patricks_form_privatemsg_new_alter($form, &$form_state) {
  ...
  $form['#submit'][] = 'patricks_privatemsg_submit';
  return $form;
}

Then in your submit handler you can use the rules_invoke_event():

function patricks_privatemsg_submit($form, &$form_state) {
   drupal_set_message(t('The form has been submitted.'));
   // Pass parameters for the variables provided by this event, as defined in hook_rules_event_info()...
   rules_invoke_event('privmsg_foo_rule', $foos, $barf, $bam);
}

If for some reason you get a rules_invoke_event() function not found error, just add a module_load_include() to your submit handler before invoking the rule.

| improve this answer | |
  • 1
    Thanks, good solution. I ultimately moved to using Login One Time module instead of Tokenauth since this provides users with more security (login links expire, whereas tokens are forever). – Patrick Kenny Aug 23 '13 at 16:52

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.