Development

Overriding a Webform Confirmation Message in Drupal 8

There are a few reasons you may need to override a webform confirmation:

  • Out of the box webform doesn’t allow you to add some tags (e.g., <button>) to a webform confirmation message. When you save it strips them out.
  • What if you needed to alter the webform message based on the values submitted in the webform?
  • What if you needed to alter the webform message based on where the user saw the webform?
  • etc…

Thankfully, you can use a standard preprocess hook to override the confirmation output.

In this particular example I needed to override the output of the message to include a button tag. I needed this to only happen if specific GET params had specific values.

First, I modified the confirmation message settings as shown here:

I like to leave helpful hints for the site administrators wherever possible. Because I’m overriding this field anyway, it is useful to explain what’s happening. The administrator will know the message is being overridden by mymodule_preprocess_webform_confirmation() .

Next, I wrote the code. In my case it ended up looking something like this:

Until I set the #allowed_tags element I was unable to get the button tag to render (Drupal was stripping it out and just showing the text).

Finally, I cleared the caches and tested the solution. Success!

 

Leave a Reply

Your email address will not be published. Required fields are marked *