Token replacement doesn't work when empty

Created on 25 August 2023, about 1 year ago
Updated 3 January 2024, 11 months ago

Problem/Motivation

Subscribers can enter first name, last name and job title. The fields are named

field_firstname, field_lastname, field_jobtitle.

I built a greeting line
Welcome, [simplenews-subscriber:field_jobtitle] [simplenews-subscriber:field_lastname]!

Sending the newsletter, only fields wich have content are substituted, maybe resulting in a line like
Welcome, Director Jones!

However, if a field hasn't been filled in, the token is printed:
Welcome, [simplenews-subscriber:field_jobtitle] McNeal!

This worked since I send mail with simplenews, at least for a decade, but it is broken with 3.0.0-beta5. It may be broken since a much earlier release, as I didn't debug token replacement for a while.

Steps to reproduce

I spotted the issue with code which used to work for at least three years, and I set up a newsletter/subscription on a 9. for development after spotting this issue.

Proposed resolution

Check for the content of a field and don't output anything in case it's empty. Earlier versions outputted a space, which lead to adjacent spaces when there were no replacements, which looked odd.

Final Question

It would be nice to run Views on the subscriber list and output to simplenews. This would allow the use of well proven views methods to create personalized newsletter which are then themed and send by simplenews / mailer. Is this possible already, is it in consideration or does it remain just a pipedream I began dreaming a decade ago?

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇦🇹Austria nofue

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

Comments & Activities

  • Issue created by @nofue
  • 🇬🇧United Kingdom adamps

    As far as I can see this has been like this since the start of the D8 branch. The code is in MailEntity::getBodyWithFormat(). Passing the 'clear' option to token replace would fix it to work how you want.

  • 🇦🇹Austria nofue

    Great -- and where/how do I pass a "clear" option?

  • 🇬🇧United Kingdom adamps

    In $options when calling \Drupal::token()->replace().

    It's strange - I would expect [entity:field_xxx] to return blank if the field was empty and that it would only get left in when the field didn't even exist on that entity.

  • 🇨🇭Switzerland berdir Switzerland

    There's an issue for that in the token module.

  • 🇬🇧United Kingdom adamps

    Thanks @Berdir - I guess it's this issue: #2648180: Replace field tokens by empty strings when there is no field value

    So maybe we close this one as "working as designed" and anyone interested can put a patch for the token module fix?

  • 🇦🇹Austria nofue

    Well, seems the token field issue has't been resolved in years, so I'm not going to give that a try. I rather write module which puts all the filled in fields from the subscription form in the right order to make for a single valid greeting field … Thanks for the heads up anyway, maybe I get good enough with coding to tackle that issue in the token module - but please don't hold your breath.

Production build 0.71.5 2024