As mentioned here, there is a request to be able to choose a referenced entity field and from that referenced entity, choose an email field to send to.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

scott_euser created an issue. See original summary.

broon’s picture

Here's a patch to tackle this issue.

How does it work?
On the Contact Email settings form I expanded the options and added a new one "Send this email to the value of a specific field in an entity reference".
The module retrieves all entity reference fields from the contact form and checks whether or not the referenced entity type (or bundle) got any email fields set up. These "referenced" email fields are turned into options in the settings form.

Everything is pretty much in line with the recently added option to choose an email field from the contact form, just added one level of complexity. It currently works for my use case as described in #2826305: Ability to send a mail to a custom field on a contact form rather than just the sender field:

That way, you could create a content type "team" which describes various team members. This CT has an email field containing the team member's email address. In a team contact form, you would add a field "team member" (reference to nodes of type team) where the user can select a team member (s)he wants to get in contact with (the selection could btw be automated with Prepopulate module).
In the Contact Email settings, you would define the "team member" field as recipient, so the contact message gets always send to the team member selected in the contact form.

This way, the email address of the team member stays hidden which is paramount these days.

scott_euser’s picture

Looks good, thanks for the contribution!
I've just updated it to:
- fix the phpcs issue of inline comments
- handle if the referenced entity's email field has a cardinality of more than 1
- changed the description to make it clear that the form needs both a reference field and an email field within that reference field

scott_euser’s picture

Ah and I removed your $debugs, they didn't seem to be doing anything.

broon’s picture

All valid points, thanks for reviewing. And the debug lines were just the ones I missed during cleaning up, thanks for spotting.
There's nothing to add from my side right now, looks like it could be committed.

  • scott_euser committed d75a0ce on 8.x-1.x
    Issue #2833627 by Paul Broon: Ability to send email to an email field...
scott_euser’s picture

Version: 8.x-1.5 » 8.x-1.6
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.