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.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | simplenews.subscribe-token.3168355-6.patch | 1.03 KB | adamps |
Comments
Comment #2
PhilippVerpoortComment #3
PhilippVerpoortComment #4
adamps commentedGood idea.
Comment #5
adamps commentedTrue, 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.
Comment #6
adamps commentedComment #7
adamps commentedComment #9
adamps commented