I need to address my subscribers in a more personal way. Thus I added fields for Mr./Mrs., firstname, lastname to the subscription block, and data gets collected nicely.
However, I can't see how to theme these fields in simplenews-newsletter-body.html.twig.

In the header of this template there's an example printing the subscriber's (built-in) mail.

 {{ simplenews_subscriber.getMail  }}

Trying the same as {{ simplenews_subscriber.field_lastname }} doesn't do anything. I tried a lot of variations to this approach, but to no avail.

What's the secret of getting custom subscriber fields to print with Twig?

  • 1
    Have you looked at the original simplenews-newsletter-body template. Usually a template has documentation at the top of the file that specifies which variables are available, etc. – sonfd Jun 30 at 17:09
  • Thanks for the hint, and yes, I themed simplenews since Drupal 6, but I don't get the TWIG way to access custom fields. Built-in fields are covered by the .get<Variable> method, which doesn't work for me (the way I try it, at least). I can see all the variables I created dumping the simplemail_subscriber variable, but I'm totally clueless as of how to access these from my twig code. – nofue Jun 30 at 17:52

{{ simplenews_subscriber.field_lastname.value }}

In the header of this template:

Note that depending on the used caching strategy, the generated body might be used for multiple subscribers. If you created personalized newsletters and can't use tokens for that, make sure to disable caching or write a custom caching strategy implemention.

To solve it I added in the sites/default/services.yml

  simplenews.mail_cache: ~
    class: Drupal\simplenews\Mail\MailCacheNone
| improve this answer | |
  • Hi @cuman, welcome to Drupal Answers. You seem to be quoting something, but you haven't sourced your quote. – sonfd Aug 6 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.