0

I'm using the core contact forms module in Drupal 8, and what I need to do is add a select with multiple options, each option needing to change the recipient address.

How can I do this using the built-in module, or do I need a different module?

2 Answers 2

3

The contact_storage module has a feature that allows you to create a list that include a key, value and e-mail and then the mail is sent to that.

You can either use that, or you can do it yourself in a similar way. You could use a term reference for example and have a field on your terms for the e-mail. The relevant code is in contact_storage_mail_alter():

$contact_message = $message['params']['contact_message'];
foreach ($contact_message->getFields() as $field) {
  if ($field->getFieldDefinition()->getType() === 'contact_storage_options_email') {
    // One of the Option email item fields. Add the recipient to the message.
    foreach ($field as $delta => $item) {
      $label = $item->value;
      // Obtain the email to add to the message, using the label.
      $email = $item->getFieldDefinition()->getSetting('allowed_values')[$label]['emails'];
      $message['to'] .= ',' . $email;
    }
  }
}

}

1
  • I worked it out using the field Contact Storage gives you. I've answered my question with the way I used. I'll upvote your answer though. Commented Aug 30, 2016 at 14:38
3

After a lot of playing around and digging about, I've actually found out that it's quite easy to do this. After installing the Contact Storage module, when editing a contact forms' fields, you can add a field named Options email.

You then are given a large text area to input keys, labels and email addresses.

Support Query|Support Query|[email protected]
Sales Enquiry|Sales Enquiry|[email protected]
Admin/Accounts Query|Admin/Accounts Query|[email protected]
Partner Query|Partner Query|[email protected]

After adding the above, I was given a select to pick from one of my options. This successfully changed the email recipient.

3
  • I'm using this and it seems to work. One issue is that it won't accept a comma separated list of email addresses
    – James
    Commented Oct 21, 2016 at 11:57
  • I've tried this too, but after one message was sent you can't change the list anymore... :/ And also every e-mail is still sent to the main address of the contact form.
    – 0711master
    Commented Feb 23, 2017 at 12:37
  • @0711master, I thought this was true as well, but despite the message saying "There is data for this field in the database. The field settings can no longer be changed.", you can in fact still add more "key|label|email" entries on the Field Settings page successfully by adding them and clicking "Save field settings". It works! Commented Apr 14, 2017 at 15:25

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.