Recipient Type Reference does not work with Computed fields

Created on 2 July 2020, about 4 years ago
Updated 21 February 2024, 7 months ago

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) {
πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • First commit to issue fork.
  • Merge request !11Issue #3156538 - Add computed fields β†’ (Merged) created by AstonVictor
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MySQL 5.7
    last update 8 months ago
    11 pass
  • πŸ‡ΊπŸ‡¦Ukraine AstonVictor

    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.

  • Status changed to Fixed 8 months ago
  • πŸ‡ΊπŸ‡¦Ukraine AstonVictor

    Merged the MR.

    Changes will be added to the next release.

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

Production build 0.71.5 2024