Access submitted form data in custom callback before hook_mail_alter() I wan't to alter contact form recipient according to what user chose in one of the dropdown fields of my Contact form.

I implemented hook_form_FORM_ID_alter() in order to add custom submit callback like this

 $form['actions']['submit']['#submit'][] = 'my_custom_callback';


to get submitted data.

I thought I could use State API in my_custom_callback() to store received data temporally and use it's value in hook_mail_alter() but it looks like hook_mail_alter() is called before my custom callback.

This is what the order of callbacks looks like:


Default Submit callback of Contact form;
hook_mail_alter();
my_custom_callback.


I wander if there is any way to change this order to this:


my_custom_callback;
Default Submit callback of Contact form;
hook_mail_alter().
Why do you need a submit callback at all? The built contact message is available in hook_mail_alter(), you can just access the submitted data there?

This is from a custom project where we did something similar:

/**
 * Implements hook_mail_alter().
 */
function yourmodule_mail_alter(&amp;$message) {
  if ($message['id'] == 'contact_page_mail') {
    /** @var \Drupal\contact\Entity\Message $contact_message */
    $contact_message = $message['params']['contact_message'];

    if ($contact_message-&gt;bundle() != 'feedback') {
      return;
    }

    $recipient_address = $contact_message-&gt;get('yourfield')-&gt;value;
    if (\Drupal::service('email.validator')-&gt;isValid($recipient_address)) {
      $message['to'] = $recipient_address;
    }
  }
}