Problem/Motivation

Using the [simplenews-subscriber:subscribe-url] and [simplenews-subscriber:unsubscribe-url] tokens in the combined confirmation email results in an error.

Here is the stack trace:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function id() on null in simplenews_tokens() (line 103 of modules/custom/simplenews/simplenews.tokens.inc).

simplenews_tokens('simplenews-subscriber', Array, Array, Array, Object)
call_user_func_array('simplenews_tokens', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('tokens', Array) (Line: 1038)
token_tokens('simplenews-subscriber', Array, Array, Array, Object)
call_user_func_array('token_tokens', Array) (Line: 403)
Drupal\Core\Extension\ModuleHandler->invokeAll('tokens', Array) (Line: 304)
Drupal\Core\Utility\Token->generate('simplenews-subscriber', Array, Array, Array, Object) (Line: 196)
Drupal\Core\Utility\Token->replace('We have received a request for the following subscription changes for [simplenews-subscriber:mail] [...] [simplenews-subscriber:subscribe-url]', Array, Array) (Line: 983)
simplenews_token_replace_body('We have received a request for the following subscription changes for [simplenews-subscriber:mail] [...] [simplenews-subscriber:subscribe-url]', Array) (Line: 123)
Drupal\simplenews\Mail\MailBuilder->buildCombinedMail(Array, Array) (Line: 562)
simplenews_mail('subscribe_combined', Array, Array) (Line: 271)
Drupal\Core\Mail\MailManager->doMail('simplenews', 'subscribe_combined', 'philipp.verpoort@mailbox.org', 'de', Array, 'no-reply@music-and-al.de', 1) (Line: 179)
[...]

Steps to reproduce

Add the [simplenews-subscriber:subscribe-url] token to the template text of the combined confirmation email. Enter an email address into the subscription block and submit. (Do not turn the email verification off.) This should result in the error, for which the stack trace is provided above.

Proposed resolution

Here is the code that results in this error from file simplenews.tokens.inc, along with a potential fix.

  case 'subscribe-url':
    #if(!$newsletter) { break; } // adding this break statement should resolve the bug
    $hash = simplenews_generate_hash($subscriber->getMail(), 'add');
    $bubbleable_metadata->setCacheMaxAge(0);
    $route_parameters += [
      'action' => 'add',
      'newsletter_id' => $newsletter->id(), // this is line 103 where the error occurs
      'hash' => $hash,
    ];
    $replacements[$original] = Url::fromRoute('simplenews.newsletter_confirm_subscription', $route_parameters, $url_arguments)->toString();
    break;

In addition to this simple fix, it is perhaps worth considering if this token and the corresponding route are actually still require given that all subscriptions are now confirmed using the combined confirmation emails.

Remaining tasks

Wait for #3066314: Subscriber updates should be confirmed atomically to be resolved. Then write a patch.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

PhilippVerpoort created an issue. See original summary.

PhilippVerpoort’s picture

Issue summary: View changes
adamps’s picture

Version: 3.x-dev » 4.x-dev

Good idea.

adamps’s picture

Priority: Minor » Normal

it is perhaps worth considering if this token and the corresponding route are actually still require given that all subscriptions are now confirmed using the combined confirmation emails.

True, it's not so easy to think of ways to use it. Here's a slightly contrived one: when sending an extra copy of a newsletter to someone who is not subscribed it could contain a link inviting them to subscribe. Probably we might as well leave it just in case someone is using it.

adamps’s picture

Status: Needs work » Needs review
StatusFileSize
new1.03 KB
adamps’s picture

  • AdamPS committed 74f4f16f on 4.x
    Issue #3168355 by AdamPS, PhilippVerpoort: Error when using [subscribe-...
adamps’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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