1

currently I'm using two hooks in my hook.module. hook_form_alter() and hook_mail_alter().

In hook_form_alter() I use drupal_get_messages() to retrieve the status message.

And I want to use hook_mail_alter() to add this message to the email. But I can't pass the message content from one hook to the other.

A global variable is not working. Is there a better way? Another hook I'm not aware of?

1

I suppose you have tried to use drupal_get_messages() within hook_mail_alter() as well?

The reason it probably did not return a value is because the default behavior or drupal_get_messages() is to clear the message queue (so it empty the second time). You can however make it persistent by passing FALSE as a second parameter (see the documentation).

So in your hook_form_alter() use drupal_get_messages(NULL, FALSE) and then you should be able to use it again in hook_mail_alter().

  • Unfortunately I can't get the messages from within hook_mail_alter() because it is a form in a form. The outer form has the status message and the inner form is sending the mail. (And I'm not aloud to edit comments) – J. Dough Dec 23 '16 at 13:54
  • Have you tried it? The status messages are retrieved with drupal_set_message() and that pulls them from $_SESSION['messages']. As long as you do not reset the queue it should be available in every function. (Assuming you do not use iframe to other sites, then there is nothing you can do) . – Neograph734 Dec 23 '16 at 14:03
  • Hm, the whole reason I started fiddeling with two hooks was because drupal_get_messages() returned only an empty array. I'm not retrieving them anywhere but in hook_mail_alter(). – J. Dough Dec 23 '16 at 14:16
  • They are displayed by the form before I'm trying to get them with drupal_get_messages(). Does this pull them out of the $_SESSION['messages'] too? If so, how could I prohibit this behaviour? – J. Dough Dec 23 '16 at 14:18
  • The messages are only available during the request in which they are generated. If you load multiple pages (eg subsequent form submissions) they are gone. You should send the mail in the same request in which the messages are generated. – Neograph734 Dec 23 '16 at 14:26

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.