0

I have a form that I want to add JS to so that when it is submitted, the confirmation text is fired into the "status messages" block or any block in the help/admin block section.

Basically, here is the form and upon submit, I want the post message to display in a block rather than the user be sent to another page.

<form name="email_signup" action="http://example.com">
<input type="text" name="emailAddress" placeholder="Enter email">
<input type="submit" value="Join">
</form>
2

You should create this form through the Form API in a module, then in the form submit handler you can use Messenger to do this.

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Messenger%21MessengerInterface.php/function/MessengerInterface%3A%3AaddMessage/8.6.x

You can see that drupal_set_message now routes through it:

function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) {
  @trigger_error('drupal_set_message() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \\Drupal\\Core\\Messenger\\MessengerInterface::addMessage() instead. See https://www.drupal.org/node/2774931', E_USER_DEPRECATED);
  $messenger = \Drupal::messenger();
  if (isset($message)) {
    $messenger
      ->addMessage($message, $type, $repeat);
  }
  return $messenger
    ->all();
}

This is how flash/status updates are created.

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.