Add token replacement support to test emails for related entities

Created on 26 July 2022, almost 2 years ago
Updated 13 February 2024, 5 months ago

Problem/Motivation

We introduced the concept of related entities in #3145846: Support adding additional token types to email token replacement β†’ to support email events that need token replacement from arbitrary related entities. For example, a site may have a custom email event plugin for subscriptions that adds token replacement support for a newly created subscription to an email triggered by an order event.

The way we added support for this right now was to identify the type IDs in the email event plugin for the sake of token replacement and to duplicate the method of extracting them from the event object. That event object does not exist during test email form submission, so we need a new way to ensure a proper reference.

Steps to reproduce

You would need a custom event plugin type to see this, but you could edit the OrderPlaced plugin to include the following two functions:

  /**
   * {@inheritdoc}
   */
  public function getRelatedEntityTypeIds() {
    return ['commerce_product'];
  }

  /**
   * @inheritdoc
   */
  public function extractRelatedEntitiesFromEvent(Event $event) {
    return [\Drupal::entityTypeManager()->getStorage('commerce_product')->load(1)];
  }

The test email form shows help text linking to this issue to explain that certain tokens may not be replaced when the form is submitted.

Proposed resolution

The simplest solution would be to add additional entity reference fields to the form for each of the related entity types. They could then be included in the replacement routine on form submission.

Alternately, we could rethink the way related entities are extracted. For example, it may be that instead of extracting related entities from the event object we should be loading them based on the already extracted primary entity. The problem is the event object may have more data in it that is used by the email event plugin to load any necessary related entity. For this reason, I tend to favor the first approach.

πŸ“Œ Task
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States rszrama

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.

Production build 0.69.0 2024