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?
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;
}
}
}
}
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.