Score:0

How get the info of a element in webform email twig?

br flag

I'm on D9, theming a webform email.

I'm currently overriding the twig file, instead of using the configuration of the body in the email hanlder, because there are various conditional logic I've to check - e.g. if interface language is en print element a otherwise element b.

To create the required markup, I need to get the info of the elements: for example, for select elements, I need the label and the text of the option selected, not the raw value.

However, I can't find them using the webform_submission, as it seems to contain only the raw data put by the user.

For now, I've find this way to get the info I need, but I'd like to know if there's a more direct\better way to get them:

webform-email-message-html--webform-id.html.twig:

{% set submission_data =webform_submission.getData %}
{% set webform = handler.getWebform %}
{% set my_element_test = webform.getElementDecoded('element_id') %}
{% set option_label_test = my_element['#options']['options_id'] %}

{# todo print the markup for all elements #}

Score:0
br flag

For anyone needing this, doing this in the twig works:



{% set webform = webform_submission.getWebform %}
{% set webform_element = webform.getElement(key) %}
{% set submission_data =webform_submission.getData %}

//value = value of the specific element inside submission_data
{% if webform_element['#options'] is defined %}
  {{ webform_element['#options'][value] }}
{% else %}
  {{ value }}
{% endif %}

Doing a bit of code-diving, I've seen that WebformOptionsHelper::getOptionText() - called at some point by the code ran by Webform tokens' function - does a bit more complex logic to get the values, but at least for my case the above is sufficient.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.