Score:-2

How can i subscribe/unsubscribe to a simplenews newsletter programmatically?

us flag

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?

ru flag
This code does not prove at all that the property `system_name` exists. The array values resulting from `$query->execute()` are always [arrays of entity IDs](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21Query%21Null%21Query.php/function/Query%3A%3Aexecute).
Score:-2
us flag

It seems that chatGPT and Drupal are not best friends, so i had to find the solution the oldschool (long & painful) way. The solution was to provide the right subscribe() arguments, so i didn't need the $newsletter.

$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');

    if ($value == 'on') {
        $subscription_manager->subscribe($user_load->getEmail(), $system_name);
    } else {
        $subscription_manager->unsubscribe($user_load->getEmail(), $system_name);
    }
}
apaderno avatar
us flag
There is no need to put the line initializing `$subscription_manager` inside `foreach()`. It is sufficient to initialize the variable once.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.