0

Whereever you are, I hope you're fairing well under the circumstances. To some of the hardest hit areas, we are thinking of you constantly.

Webform Advanced Elements

I am having difficulty retrieving submitted values from Drupal 8.8.4 Webform 5.9 multipart elements using Twig. Standard, single form field, values work fine, however elements such as Name and Advanced Address do not. Both of latter elements have multiple form fields (e.g. first, middle, last name; or street, city, country...) and may be dynamic (e.g. pick a country before completing address).

In my case, I am using the Attachment PDF and a custom Twig template to generate a PDF file attachment that is returned to the user, however I can see the same result using Twig in an e-mail, or the generated test e-mail preview.

Testing:

A simple text field works correctly:

<p>Dated: {{ data.date }}</p> (works)

Name and Advanced Address are blank:

<p>{{ data.my_name }}</p> (blank)

<p>{{ data.my_address }}</p> (blank)

Tokens Work, Sort Of

For what it's worth, the multipart values are available using tokens in the e-mail body, albeit I can't get portions of the values, which is the objective. What I mean by this is that an entire complete address will be returned, on multiple lines, but getting just the street name doesn't work (for me).

Using the examples above:

[webform_submission:values:my_name] (returns John Smith)

It also wasn't possible to retrieve just a street address:

[webform_submission:values:address] (returns stree, city, state/prov., country on mutiple lines)

I've really been stumped, and I'm surprised it is so hard to find the answer. Perhaps I'm not working the question correctly, this is more of a hobby project. Thank you for suggestions.

2 Answers 2

0

I think you are just overlooking something simple here. The 'multi part form elements' is called a composite element. And it holds an array of values of its subfields.

Let's say the composite parent is called 'name', and it has a subfield called first_name, then you can use {{ data.name.first_name }} to get the first name in twig.

As for webform submission token, you can try something like [webform_submissions:values:name:first_name]

2
  • Hi, thanks cchen, it is a valid point, but I don't know what the reference names for the array. I have tried .first .given .first_name .given_name, and likewise for the address composite element.
    – gnurob
    Mar 29, 2020 at 0:55
  • Actually, reading a bit more about this, aren't composite elements in relation to being able to add more than one answer (i.e. cell, home, work) and then referring to [webform_submission:values:telephone:0] and [webform_submission:values:telephone:1]?
    – gnurob
    Mar 29, 2020 at 1:05
0

Ok, I kept trying cchen's suggestion and made some discoveries.

Twig

Since the following did not work, I did not try sub-elements.

{{ data.my_address }}

Although the whole element does not work (whole elements work with tokens) the sub-elements are still addressable. You can find the sub-element names by looking at submitted form results, under Data (YAML) tab.

{{ data.my_address.address_line1 }}

Lesson learned: don't give up.

Tokens

The following works. I have no idea how I missed this in testing.

[webform_submission:values:my_name:first]

With respect to the address example, the correct field name was address_line1, and it was not one of the variations I guessed (e.g. address, address1, street1, etc.)

Thanks all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.