Problem/Motivation

If the token type is not exactly the same as the variable name, the variable is not passed.

I encountered this problem with simplenews and the simplenews-subscriber token type. The variable is called simplenews_subscriber and is therefore not included in the data variable passed to the token service.

Steps to reproduce

Install the simplenews module and sent a subscription email using the subscription block.
All [simplenews-subscriber:....] are not convert.

Proposed resolution

As a workaround, and so as not to change anything, I added a fake token type to the MailerInfo of simplenews.subscriber:

#[MailerInfo(
  base_tag: "simplenews.subscriber",
  label: new TranslatableMarkup("Simplenews subscriber"),
  sub_defs: [
    "subscribe" => new TranslatableMarkup("Confirmation"),
    "validate" => new TranslatableMarkup("Validate"),
  ],
  required_config: ["email_subject", "email_body"],
  token_types: [
    "simplenews-subscriber",
    "simplenews_subscriber",  <-- Quick fix
  ],
)]

It's not a permanent solution, but it does the trick.

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

damien laguerre created an issue. See original summary.

damien laguerre’s picture

Title: Token replacement does not » Token variables are not passed if they do not have the same name as the token type.
Issue tags: -Token variables are not passed if they do not have the same name as the token type. +tokens
adamps’s picture

Thanks for the report. Yes the problem comes from the inconsistency in simplenews tokens, summarised by simplenews_token_info().

  $types['simplenews-subscriber'] = [
    'name' => t('Simplenews subscriber'),
    'description' => t('Tokens related to the newsletter recipient'),
    'needs-data' => 'simplenews_subscriber',

I suggest a slightly different fix on line 47:

-      'simplenews_subscriber' => NULL,
+      'simplenews-subscriber' => $subscriber,

If anyone can test it, or create a MR that would be a big help.

adamps’s picture

Title: Token variables are not passed if they do not have the same name as the token type. » Tokens missing for simplenews subscriber mails when overridden
Priority: Normal » Major

I confirm the bug. The subscribe and validate emails are unusable for anon subscribers.

adamps’s picture

Status: Active » Needs review

Sorry the suggestion in #3 was a bit muddled (I was mostly looking at the wrong file SimplenewsNewsletterMailer.php). It had the right idea to call ->setParam('token_data', and this is now done correctly in the MR.

adamps’s picture

The parts of the MR relating to use AutowireTrait; aren't part of this fix - it's just some tidy-up I noticed recently and didn't want to create a whole separate MR/issue for.

  • adamps committed 1bc3b76d on 2.x
    Bug: #3566176 Tokens missing for simplenews subscriber mails when...
adamps’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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