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 #3404625: Email body policy form should show available TWIG variables for the variables.

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 tokens
  2. Look at the code in the EmailBuilder. The tokens are normally set with setParam() or this can be overridden with tokenData().

Proposed resolution

1) Add new parameter to the @EmailBuilder annotation: token_types = array of types to show in token browser.
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 these annotations in BodyEmailAdjuster::settingsForm(). Create a token browser with '#theme' => 'token_tree_link', and set #token_types based on the token_types annotation.

Example

UserEmailBuilder:

token_types = {"user"}

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.

adamps’s picture

The module business_rules has a nice example. It shows the available twig variable, and also shows the fields available on each, a bit like the token browser.

nchase’s picture

A token browser would be really helpful.

featuriz’s picture

I'm so confused with policy.

<p class="contact-email-intro"><a href="{{ sender_url }}">{{ sender_name }}</a> sent a message using the contact form <a href="{{ form_url }}">{{ form }}</a>.</p>
<span>Name:-</span><span>{{ field_name }}</span>
<span>Name:-</span><span>{{ field_name_value }}</span>
<span>Email:-</span><span>{{ contact_message_field_email }}</span>
<span>Phone:-</span><span>{{ contact_message_field_sender_phone }}</span>
<span>Message:-</span><span>{{ contact_message_message }}</span>
<span>Message:-</span><span>{{ contact_message_message_value }}</span>

NOTE: In my gmail - I can see only the paragraph tags content rendered correctly. All the other things in spam twig content are empty.

My token for email field is: [contact_message:field_email]

Nothing works for me. help needed. Because there is no such thing in google search, I can't find any helpful links.

sachbearbeiter’s picture

+1 for this feature ...

thalemn’s picture

+1 for this feature

anche’s picture

@featuriz did you manage to solve the problem?

anche’s picture

+1 for this feature

adamps’s picture

Please no more "+1 for this feature"😃. It needs a volunteer to write the code (or to offer sponsorship).

abyss’s picture

Assigned: Unassigned » abyss

I will try to add the described functionality

adamps’s picture

Issue summary: View changes

Great thanks - I added some more information to the Issue Summary.

anche’s picture

Assigned: abyss » Unassigned
Issue summary: View changes

while we are waiting for volunteers, can at least someone explain where to get tokens or twig variables for email body in policy?

adamps’s picture

Issue summary: View changes

@Anche Good question - I added a "workaround" heading to the Issue Summary.

anche’s picture

@AdamPS Thanks! Do I understand correctly that before you get the necessary twig variables for your form, you need to programmatically set them? That is, if you are not good enough in programming, symfony mailer will not help you in any way to send emails in html format?

adamps’s picture

Do I understand correctly that before you get the necessary twig variables for your form, you need to programmatically set them?

No, the variables will already be set automatically. The purpose of this issue is to inform people using the GUI what variables are available.

adamps’s picture

Issue summary: View changes
adamps’s picture

Issue summary: View changes
coaston’s picture

AdamPS so how to add tokens to email body ? I am confused.

vlad.dancer’s picture

Here is a cheap way to add token browser on mailer policy edit form:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function symfony_mailer_form_mailer_policy_edit_form_alter(array &$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id): void {
  if (\Drupal::moduleHandler()->moduleExists('token')) {
    $form['config']['token_browser'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => 'all',
      '#global_types' => FALSE,
      '#dialog' => TRUE,
    ];
  }
}

I'm not a fan of Token UI but at least then we will have tokens list at the hand.

tomsaw’s picture

Thanks @vlad.dancer, this is simple and does the job for me!

adamps’s picture

Status: Active » Needs work

Thanks it's a good start. Please see the issue summary for the next steps. I guess the MR will only show the global tokens.

abyss’s picture

Assigned: abyss » Unassigned
Status: Needs work » Needs review

@AdamPS, I added the token list display in the BodyEmailAdjuster.
About adding an annotation, it seems impossible at the moment, because Adjusters don't know anything about the place where they are called, namely EmailBuilder. So, we had to abandon this idea.
If you have any other suggestions, I will be happy to add them.

adamps’s picture

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

The annotation is on @EmailBuilder instead of the adjuster. Please check the issue summary. It also explains how to adjust 2 of the email builders. Some of the others will need adjusting too.

EDIT: deleted rest of answer which is wrong😃 Updated IS instead

abyss’s picture

However, in this case, this data will be available only at the stages of building an email, which will no longer be valuable to us.
The data about the current EmailBuilder is not contained in the settingsForm stage, and if I understood the existing code correctly, it cannot be contained there.
If I've misunderstood something, please correct me.

Alternatively, we can store these values in the form_state in the PolicyEditForm and retrieve them from the form_state in the BodyEmailAdjuster.
In this case, to display the available tokens by type, we can indeed describe token_types, but I don't see how we can use variables in this context.

adamps’s picture

Issue summary: View changes

Ha I just realised my answer was wrong, however you beat me to it😃. I updated the IS with an explanation as you said.

adamps’s picture

StatusFileSize
new29.77 KB

For the variables, I imagined something like on a view. Configure a field, select "Override the output of this field with custom text" and expand the box "REPLACEMENT PATTERNS". I uploaded a screenshot. It's completely separate from the token browser, just a list of variables with a description of each.

abyss’s picture

If I understood correctly, it doesn't seem logical to do this in BodyBuilder.
In this case, the user will still have to write a handler for their own twig tokens, which is similar to creating their own token.
So do you mind if we add only token_types?

adamps’s picture

Yes I would accept a patch for only tokens.

I would create a follow-on issues for the variables. I don't understand what problems you see for them, please could you explain more?

For example, in ContactEmailBuilder::build() there is this code:

    $email->setVariable('recipient_name', $recipient->getDisplayName())
      ->setVariable('recipient_edit_url', $recipient->toUrl('edit-form')->toString());

The @EmailBuilder annotation would then have a line for each variable giving a description. This allows the form to tell the user the available variables.

abyss’s picture

I'm afraid I misunderstood you a bit.
In this context, it makes sense to add it.
Then do we need to add something else in this task?

adamps’s picture

Issue summary: View changes

Great. You can choose: do both token and variables in this issue OR have one issue for each of them. I added some more notes to the IS.

abyss’s picture

If so, let's split it into two tasks and I'll add this functionality tomorrow.

abyss’s picture

@AdamPS, could you separate these tasks then?

adamps’s picture

Title: Email body policy form should show available TWIG variables or token browser » Email body policy form should show available TWIG variables
Issue summary: View changes
Related issues: +#3404625: Email body policy form should show available TWIG variables
abyss’s picture

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

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

mav_fly’s picture

I did the propsed patch but after applying this patch i got the follow error :

Call to undefined method Drupal\symfony_mailer\Entity\MailerPolicy::getVariables()
line 30 --> www/modules/contrib/symfony_mailer/src/Form/PolicyEditForm.php.

When set line 30 in comment $form_state->setValue('variables', $this->entity->getVariables());
The error disappears, but when you click on available tokens the popup windows appears but it's empty .

Maybe I forgot something, can anyone help me to solve my problem ?

adamps’s picture

Title: Email body policy form should show available token browser » Email body policy form should show available token browser and TWIG variables
Version: 1.x-dev » 2.x-dev
Assigned: Unassigned » adamps

I am working on this and also the TWIG variables, so let's combine the issues again.

Many thanks to @abyss for the valuable work on the MR in the other issues.

adamps changed the visibility of the branch 3270408-email-body-policy to hidden.

adamps changed the visibility of the branch 2.x to hidden.

adamps changed the visibility of the branch 1.x to hidden.

adamps’s picture

Status: Needs work » Needs review

MR ready for 2.x please review and test

adamps’s picture

Component: Code » Policy
Issue tags: -Needs tests

I'm going to make a release soon, so I'll commit this now. Thanks everyone for the contributions. Please do test and give any feedback if anyone gets a chance.

We're missing any kind of functional test for mailer policy, so there's not yet a framework to a new test into. Unfortunately I don't have time to fix that right now, so let's just get this in and I'll continue work towards a 2.x stable release.

  • adamps committed ca717b0a on 2.x
    Issue #3270408 by adamps, abyss, vlad.dancer: Email body policy form...
adamps’s picture

Assigned: adamps » Unassigned
Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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