Problem
The "Edit Contact Email" allows you to select "The value of a specific field in an entity reference"; however, this does not work if the "specific field" is a computed field.
This is because in ContactEmails::getEntityReferenceEmailFields, the field definitions from EntityFieldManager are filtered by whether or not they are instances of FieldConfigInterface. Computed fields are not instances of FieldConfigInterface, but BaseFieldDefinition.
Resolution
I am not sure why the original code filters by FieldConfigInterface - maybe to exclude base fields? I have tested this with the patch below and it seems to work fine, and I think this is the most conservative change to make this work.
diff --git a/src/ContactEmails.php b/src/ContactEmails.php
index 7e590c6..25837dc 100644
--- a/src/ContactEmails.php
+++ b/src/ContactEmails.php
@@ -275,7 +275,7 @@ class ContactEmails {
foreach ($bundles as $bundle_name) {
$bundle_label = $bundle_info[$bundle_name]['label'];
$bundle_fields = array_filter($this->entityFieldManager->getFieldDefinitions($handler[1], $bundle_name), function ($field_definition) {
- return $field_definition instanceof FieldConfigInterface;
+ return ($field_definition instanceof FieldConfigInterface || $field_definition->isComputed());
});
if ($bundle_fields) {| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3156538-computed-reference-fields-2.patch | 658 bytes | csheltonlcm |
| #2 | 3156538-computed-reference-fields.patch | 659 bytes | csheltonlcm |
Issue fork contact_emails-3156538
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
csheltonlcm commentedComment #3
scott_euser commentedThanks for the patch. Not sure what a computed field is actually so I'll have to do a bit of digging but just changing to Needs review so tests run.
Comment #4
scott_euser commentedLooks like the patch isn't applying, could you check it applies to the development branch and post an update please?
Comment #5
csheltonlcm commentedWhoops, sorry about that! This patch should work.
And this is a good intro on computed fields: https://www.drupal.org/docs/8/api/entity-api/dynamicvirtual-field-values...
Comment #6
csheltonlcm commentedComment #9
astonvictor commentedCreated a new MR to support computed fields.
Steps for testing:
1. Create a new computed field of the 'email' type for the user entity type and its callback.
e.g.
2. Create a new Contact form on the
/admin/structure/contact/addpage and add a new user reference field.3. Create a new Contact email on the
/admin/structure/contact/emails/add/addpage and select the contact form from the above.4. Select the
'The value of a specific field in an entity reference'value for the'Recipient type'field.Results: you should see your computed field. Creating a new contact message should send an e-mail to the e-mail address from the computed field.
Comment #11
astonvictor commentedMerged the MR.
Changes will be added to the next release.