I have the following code to set subscriptions to a simplenews newsletter on or off:
$user_load = User::load($userID_current);
$newsletter_subscriptions = [
'newsletter1' => 'on',
'newsletter2' => 'off',
'newsletter3' => 'off',
'newsletter4' => 'off',
];
foreach ($newsletter_subscriptions as $system_name => $value) {
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
$newsletter = \Drupal::entityTypeManager()
->getStorage('simplenews_newsletter')
->loadByProperties(['system_name' => $system_name]);
if ($value == 'on') {
$subscription_manager->subscribe($user_load, $newsletter);
} else {
$subscription_manager->unsubscribe($user_load, $newsletter);
}
}
in the $newsletter
i got back NULL even though the properties exists (checked by this:
$newsletter_storage = \Drupal::entityTypeManager()->getStorage('simplenews_newsletter');
$system_names = $newsletter_storage->getQuery()
->execute();
foreach ($system_names as $system_name) {
\Drupal::logger('simplenews')->info('Simplenews system name: @system_name', ['@system_name' => $system_name]);
}
whats wrong with my $newsletter
? Does someone have experiences with simplenews 3?