Access to the form in Statamic

Pull request: add “form” to Submission array

Published June 18th, 2021

Important: The information in this article is over 12 months old, and may be out of date or no longer relevant.

But hey, you're here anyway, so give it a read see if it still applies to you.

There are a few things in the web dev world that are just annoying. File uploads, Internet Explorer and forms. But thankfully Statamic comes with a bunch of helpers to make both file uploads and forms a little easier. No one can help Internet Explorer though.

When defining the destination for your forms, you can choose the templates to use for your HTML and text based emails of the form - which is a really neat way to make emails look and feel pretty and readable for your site owners. They can even easily loop over the form’s blueprint’s fields, and makes it really easy to write generic templates for any form you might have.

But one issue I encountered was that there was no way to identify, at the template level, what form the response is coming for.


And the only way to overcome this would be to create a new template pair (one for HTML and one for text) for each form you create. Which is just a pain in the behind. Remember, forms are already annoying (see the first sentence) so let’s keep it simple, yeah?

Digging through the source code for Forms, Emails and Submissions, there was one omission: the form reference itself. While at the template level we have access to things like global config, the current date/time and the fields themselves (obviously), we don’t actually have access to the form.

So something as simple as including the form name was not possible.

But I found a way to do it… and through only a few minor changes to the code, had a pull request ready for the core statamic/cms repo.

What makes me even more excited is that it was approved, with (what I feel is) good feedback about the discussion points created, and has been released v3.1.22.

How do you use it?

Well, I’m glad you asked. And it’s really easy! Because, remember, files, Internet Explorer and forms are annoying. So let’s make forms less annoying.

This pull request passes a form variable through to the templates for HTML and text emails - and passes the full Form model.

If you have a field in your Form called “Title” (which you no doubt have), you can then output the form’s title anywhere in your HTML and text templates using:

{{ form:title }}

And you could output any other fields you need too - title is just the big one I was after.

See - how easy was that!

Note that even though there is a {{ form }} tag already in Statamic, it’s unlikely that you’d use it in the email template form - but if you need to, you can still go the long way with:

{{ form:create :in="form:handle" }}

Given my past experience with open source contribution was less than positive (but let’s leave that story for another day), this experience has given me more confidence in my contribution potential - and also open-mindedness from the Statamic team to consider features that, while seemingly small, can make a big difference to not only my dev work, but hopefully help others too.

You may be interested in...