Problem/Motivation

The description for BodyAdjuster form says:

This field may support tokens or Twig template syntax – please check the supplied default policy for possible values.

We should instead help people out. This issue covers the tokens - see #3270408: Email body policy form should show available token browser and TWIG variables for the tokens.

Steps to reproduce

Edit a policy and add a body element

Workaround

  1. Look at the default installed policy - this will often use most of the available variables
  2. Look at the code in the EmailBuilder. The variables are set with setVariable().

Proposed resolution

1) Add new parameter to the @EmailBuilder annotation: variables = array variable name => description.
2) Make the EmailBuilder definition available in the form

  • Add MailerPolicy::getBuilderDefinition()
  • Deprecate MailerPolicy::getCommonAdjusters(), as it's easy to get from the above
  • In PolicyEditForm::form(), call $this->entity->getBuilderDefinition() and store the result in the form.

3) Use the annotations in BodyEmailAdjuster::settingsForm(). Display the variables with '#type' => 'details', and set #value based on the variables annotation.

Example

ContactPageEmailBuilder:

variables = {
  "form" = "BLAH",
  "form_url" = "BLAH",
  "recipient_name" = "BLAH",
  "recipient_edit_url" = "BLAH",
}

Remaining tasks

User interface changes

API changes

Data model changes

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

AdamPS created an issue. See original summary.

abyss’s picture

Title: Email body policy form should show available token browser » Email body policy form should show available TWIG variables
Issue summary: View changes

Updated the title and description as it was misplaced with #3270408: Email body policy form should show available token browser and TWIG variables.

abyss’s picture

Assigned: Unassigned » abyss

abyss’s picture

@AdamPS, I added display of TWIG variables in the BodyEmailAdjuster.
But I would like to clarify, from which version it is better to note that getCommonAdjusters is deprecated, and in which version it should be removed?

adamps’s picture

Great thanks I'll take a look. It would be deprecated in 1.4.1 and removed in 2.0.0.

adamps’s picture

Status: Active » Needs work

Yes it looks very good. I didn't have a chance to test it yet, however I reviewed the code in the MR and made a few simple comments.

abyss’s picture

Status: Needs work » Needs review

@AdamPS, I made changes as per the comments in MR and added a deprecated message for MailerPolicyInterface::getCommonAdjusters.
If you have any other suggestions, I will be happy to add them.

adamps’s picture

Status: Needs review » Needs work

Great thanks, it's better. I tested it now and I like it. I made some more comments in the MR - they should all be quick and easy.

Two bigger comments:

1) Variables can also be used in SubjectEmailAdjuster and we should do the same. Please put the code in a function (don't just copy it😃) e.g. MailerHelperInterface::showReplacementHelp()

2) It would be great to have a test. At the end of TestEmailTest::testTest(), add a call $this->submitForm([], 'Edit'); then can check that the page contains the replacement pattern for day.

adamps’s picture

Actually the are issues open to use tokens in other places too: e.g. plain text body or addresses. So perhaps we should have the variable/token helpers on the form just once - in PolicyEditForm, after all the adjusters. Then for each adjuster that accepts tokens/variables we can put a message to say that.

adamps’s picture

OK if you can do it for the email body then that's already good. We can raise a follow on issue for the other places.

adamps’s picture

Assigned: abyss » Unassigned
Status: Needs work » Closed (outdated)

Rejoining with #3270408: Email body policy form should show available token browser and TWIG variables

Many thanks @abyss your MR was a big help.