Hi

I added a twig template for a specific form's submission to send an email with the results to a specific email address.

I had formatted the results into a set of divs etc to make the results easier to read.

Issue now is that the template been used is the one specified in the email handler.

The filename of the twig template is: webform-email-message-html--customized-email-to-admins.html.twig where "customized-email-to-admins" is the id specified in the handler details.

IS therre something else specific I am supposed to do to ensure the file is used instead of the handler Custom body? Like a HOOK with template suggestion possibly ?

Thanks
John

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

johncogan created an issue. See original summary.

jrockowitz’s picture

Status: Active » Needs review

The template and suggestions are hard to see because the email template is only rendered when an email is sent via \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::sendMessage

  • webform_email_message_html__WEBFORM_ID
  • webform_email_message_html__WEBFORM_ID__WEBFORM_HANDLER_PLUGIN_ID
  • webform_email_message_html__WEBFORM_ID__WEBFORM_HANDLER_PLUGIN_ID__WEBFORM_HANDLER_ID

Below are the template suggestions for the contact form's notification email.

  • webform_email_message_html__contact
  • webform_email_message_html__contact__email
  • webform_email_message_html__contact__email__email_notification
johncogan’s picture

Thanks. I am a little lost on what the 'WEBFORM_HANDLER_PLUGIN_ID' is exactly? I am assuming its going to simply be the same as your contact form example, i.e. 'email'

My webform id = enquire
handler id (as listed on /admin/structure/webform/manage/enquire/handlers) = customized_email_to_admins

Regards and thanks

John

johncogan’s picture

I have tried all combinations without success:

webform_email_message_html__enquire__email__customized_email_to_admins.html.twig
webform_email_message_html__enquire__email.html.twig
webform_email_message_html__enquire.html.twig

Is there a hook that I can use to 'force' it to use a specific twig file ?

jrockowitz’s picture

I think you just need to replace the underscores with dashes in your *.twig.html files.

@see https://www.drupal.org/docs/8/theming/twig/working-with-twig-templates

johncogan’s picture

Hi jrockowitz.

Many thanks for that. My template is coming through with my markup now but the issue I am having is grabbing the individual submitted values. I originally tried a direct translation of the token and then other iterations.

What I tried was:

<div class="divTableRow"><!-- [webform_submission:values:?] -->
            <div class="divTableCell headerText">First name</div>
            <div class="divTableCell userValueText">1. {{first_name}}</div>
            <div class="divTableCell userValueText">2. {{submission.first_name}}</div>
            <div class="divTableCell userValueText">3. {{submission.values.first_name}}</div>
            <div class="divTableCell userValueText">4. {{submission['first_name']}}</div>
            <div class="divTableCell userValueText">5. {{webform_submission.values.first_name}}</div>
            <div class="divTableCell userValueText">6. {{webform_submission['first_name']}}</div>
            <div class="divTableCell userValueText">7. {{items.first_name}}</div>
            <div class="divTableCell userValueText">8. {{element.elements.first_name}}</div>
            <div class="divTableCell userValueText">9. {{webform_submission.first_name}}</div>
        </div>

The first name element is a plain text field, I haven't even tried grabbing values from List items yet like Checkboxes and select lists.

But none of them work and I cannot find a working example anywhere that hints at grabbing individual element values.

Thanks
John

jrockowitz’s picture

The email templates variables are very basic. You need to define a template preprocessor to add the submission data to the $variables array.

The below code snippet would allow you to access submission data as {{ data.email }}

/**
 * Prepares variables for HTML email.
 */
function CUSTOM_MODULE_preprocess_webform_email_message_html(&$variables) {
  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $variables['webform_submission'];
  $variable['data'] = $webform_submission->getData();
}

The webform-email-message-html.html.twig template is really for just adding a custom header and footer to any sent email. You might want to consider using the Email handler's custom Twig support.

jrockowitz’s picture

Status: Needs review » Postponed (maintainer needs more info)
jrockowitz’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)
glennnz’s picture

I'm struggling with this too; trying to override the default 'webform submitted' email with a twig template. I've selected Twig as the email format and have added just, "This is broken..." to that field.

If my webform name is 'custom_webform' and I've added an email handler to that webform called 'custom_handler', exactly what should my twig file be called?

And, do I need to define this in a hook_theme()? I don't think so.

I'm getting lots of emails telling me that it's broken....

Thanks,

Glenn

glennnz’s picture

@jrockowitz

Re your comment #7, how could I make those submission data items available for use in a node that wanted to display some webform data?

Thanks.

--UPDATE--

I ended up just pushing the webform data into fields on my content type so they become available to the twig file.

glennnz’s picture

Solved for me.

Selected Twig template as the email handler body format, and named my twig file as webform-email-message-html--WEBFORM-ID.html.twig.

glennnz’s picture

Status: Closed (works as designed) » Active

@jrockowitz

I have the twig template working but can't get the webform submission data to show in the email.

I've used your code from comment #7, but can't get the data to display in the email; {{ data }} or {{data.field_name }} both show as empty in the rendered email.

Thanks.

cilefen’s picture

Status: Active » Closed (works as designed)
The Webform module's issue queue is for bugs, feature requests, and planning. Please post general support questions to Drupal Answers. You can also ask questions or help answer questions on the #webform channel on Drupal Slack.