I am using a custom composite field type to gather multiple pieces of data from a user. This custom composite field type contains a field which gathers the email addresses entered.

What I am trying to do is use a Computed Twig field to extract the email addresses entered within the custom composite field type and use an email handler to send a confirmation email to all those email addresses, however, I am having real trouble trying to reference a field inside a composite field type. I have used multiple pieces of twig code, but all I seem to be able to reference is the values from all the fields in the custom composite and not a specific field.

I am really new to twig, so do not understand the full capabilities yet, but does anyone know how to reference a field value inside a composite field type, using twig?

  • 2
    A twig template is probably not the best place for this type of logic. Are you wanting to send emails each time the entity is viewed, on cron run or something else? Then you can use the relevant hook or event to trigger the sending of emails. I can be more specific if you can provide some more details. Twig should mainly be used for presentation.
    – imclean
    Mar 16, 2020 at 21:06
  • Thanks for the tip! Essentially, the custom composite field allows a user to enter multiple entries of data, in this case, an email address. When the user submits the form, the email addresses entered would need to be compiled into an email handler, so an email can be sent to each email address entered. Are those details more helpful to narrow down a method to achieve this? I am not familiar with hooks to be honest.
    – Jon
    Mar 17, 2020 at 11:33
  • What kind of form is it? Are you creating a node/entity or submitting a webform or is it a custom form? If it's your own custom form, the submit handler for the form could be used to send the emails.
    – imclean
    Mar 17, 2020 at 21:55
  • @imclean It is just a standard form using the webform module. Usually, in the webform module, you can just insert the machine_name of the field into the email handlers 'To' field, however, because the field 'email address' is contained within a custom composite field, you cannot seem to reference the field value. What would be required to create a submit handler?
    – Jon
    Mar 18, 2020 at 15:21
  • You could use hook_form_alter() to add your own submit handler, but webform provides a pluggable handler system so it's best to use that: drupal.org/docs/8/modules/webform/webform-cookbook/…
    – imclean
    Mar 19, 2020 at 21:30

1 Answer 1


I'll provide 2 links as an answer, but it has really been answered by others.

  1. Write custom webform handler:

How to create a custom Webform handler to submit to a SOAP API

  1. Use hook_form_alter() to add a custom submit handler.

Add a custom submission handler to a form

Drupal 8 version of hook_form_alter(): https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.8.x

Your Answer

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

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