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) {
Command icon 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

csheltonlcm created an issue. See original summary.

csheltonlcm’s picture

StatusFileSize
new659 bytes
scott_euser’s picture

Status: Active » Needs review

Thanks 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.

scott_euser’s picture

Status: Needs review » Needs work

Looks like the patch isn't applying, could you check it applies to the development branch and post an update please?

csheltonlcm’s picture

StatusFileSize
new658 bytes

Whoops, 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...

csheltonlcm’s picture

Status: Needs work » Needs review

AstonVictor made their first commit to this issue’s fork.

astonvictor’s picture

Version: 8.x-1.19 » 8.x-1.x-dev

Created 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.

/**
 * Implements hook_entity_base_field_info().
 */
function {MODULE}_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];

  switch ($entity_type->id()) {
    case 'user':
      $fields['computed'] = \Drupal\Core\Field\BaseFieldDefinition::create('email')
        ->setLabel(t('Computed mail'))
        ->setCardinality(1)
        ->setComputed(TRUE)
        ->setCustomStorage(TRUE)
        ->setClass('\Drupal\{MODULE}\Field\{MODULE}EmailField');
      break;

  }

  return $fields;
}

2. Create a new Contact form on the /admin/structure/contact/add page and add a new user reference field.
3. Create a new Contact email on the /admin/structure/contact/emails/add/add page 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.

astonvictor’s picture

Status: Needs review » Fixed

Merged the MR.

Changes will be added to the next release.

Status: Fixed » Closed (fixed)

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