Allow 3rd-party to act before delivery

Created on 9 October 2022, over 1 year ago
Updated 29 January 2024, 5 months ago

TL;DR: Allow 3rd-party to act before delivery

Problem/Motivation

Here’s a use case from my project:

  • Message arguments are nice, they allow to customize the message.
  • However, when it comes to message_notify they are not enough. There's no way to add arguments describing the recipient. And such arguments are so important when you need personalised emails, for instance:

    Dear Joe Doe,
    Your balance is now $35.5

    And the message template would look like:

    Dear @recipient:name,
    Your balance is now @recipient:balance

    Such arguments are per-recipient and they need to be computed on delivery phase.

  • True, you're still able to hack in hook_mail_alter() but that doesn't look so good

Steps to reproduce

N/A

Proposed resolution

Add an API that allows 3rd-party to interact before the message is assembled. For instance, when the email plugin computes the sender and recipient emails, it's too late as the message render array has been already assembled.

Here's a proposal:

  • Just before assembling the message render array, in MessageNotifierBase::send(), dispatch an event that allows 3rd-party to add more arguments or manipulate the notifier plugin configuration. For instance, having access to the plugin configuration that already contains the recipient email, a module, listening to the event, is able to get the user full name and set a new argument in the message entity.
  • Just before event dispatching, run a protected ::init() method allowing some plugins to do preparations before the dispatcher runs. E.g. the email plugin wants to prepare the from and recipient emails and the language code as well. The default implementation, MessageNotifierBase::init(), has an empty body so that we don't break any plugin that doesn't need this method.

Remaining tasks

None.

User interface changes

None.

API changes

3rd-party are able to subscribe to an event and hook in the process just before the message render array is assembled and the message is delivered. They can:

  • Manipulate the message arguments. E.g. one can add recipient arguments such as the recipient's full name, as at this point the recipient email is known.
  • Manipulate the notifier plugin instance configuration. E.g. change the from email address

Data model changes

None.

✨ Feature request
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

πŸ‡·πŸ‡΄Romania claudiu.cristea Arad πŸ‡·πŸ‡΄

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

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