Problem/Motivation

As describe in #3016953, it would be nice to be able to use hook_mail_alter to make changes to the email based on some values/conditions of the entity. Currently the entity object isn't available in hook_mail_alter though, making this difficult.

Proposed resolution

Pass the entity object to the $this->mailManager as a param so it is available inside hook_mail_alter. This appears to be what the core Contact module does.

Should be possible by changing /src/Plugin/QueueWorker/WorkbenchEmailProcessor.php to add the entity that is already available.

// Send the email.
=$this->mailManager->mail('workbench_email', 'template::' . $template->id(), $data->getTo(), LanguageInterface::LANGCODE_DEFAULT, [
    'body' => $body,
    'template' => $template,
    'subject' => $subject,
    'entity' => $entity,
], $replyTo);

Remaining tasks

  1. Agreement on feature request.
  2. Make patch to add functionality.

User interface changes

None

API changes

None

Data model changes

None

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

PCate created an issue. See original summary.

larowlan’s picture

Sounds good

pcate’s picture

Status: Active » Needs review
larowlan’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Novice

Thanks, can we get a test to go with this.

We already have tests/modules/workbench_email_test/workbench_email_test.module so if you put an implementation of hook_mail_alter in there to add something extra to the titles based of the entity, and then modify \Drupal\Tests\workbench_email\Functional\WorkbenchEmailTestBase::testEndToEnd to check that the modification happened we should be covered

Thanks!

pcate’s picture

add something extra to the titles based of the entity

By title do you mean adding something to the node title or the email subject?

larowlan’s picture

Sorry, add something extra to the subject based on the entity

pcate’s picture

Status: Needs work » Needs review
pcate’s picture

Status: Needs review » Needs work
pcate’s picture

Status: Needs work » Needs review
pcate’s picture

Test added.

  • larowlan committed 008aaac on 2.x authored by PCate
    Issue #3203414 by PCate, larowlan: Pass entity object to MailManager as...
larowlan’s picture

Status: Needs review » Fixed

🔥hot stuff, merging this and creating a new release.

Great work 👌

Status: Fixed » Closed (fixed)

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