function simplenews_user_profile_form_submit in Simplenews 8.2
Same name and namespace in other branches
- 8 simplenews.module \simplenews_user_profile_form_submit()
- 3.x simplenews.module \simplenews_user_profile_form_submit()
Submit callback for the user profile form to save the newsletters setting.
1 string reference to 'simplenews_user_profile_form_submit'
- simplenews_form_user_register_form_alter in ./
simplenews.module - Implements hook_form_FORM_ID_alter().
File
- ./
simplenews.module, line 395 - Simplenews node handling, sent email, newsletter block and general hooks.
Code
function simplenews_user_profile_form_submit($form, FormStateInterface $form_state) {
$account = $form_state
->getFormObject()
->getEntity();
// Process subscription check boxes.
/** @var \Drupal\simplenews\Subscription\SubscriptionManagerInterface $subscription_manager */
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
// Invalid input (non-array) could result in a NULL return value, ensure to
// only load and subscribe if valid input is provided.
if (is_array($form_state
->getValue('subscriptions'))) {
$ids = array_filter($form_state
->getValue('subscriptions'));
if ($ids) {
foreach (Newsletter::loadMultiple($ids) as $newsletter) {
$subscription_manager
->subscribe($account
->getEmail(), $newsletter
->id(), FALSE, 'website', $account
->getPreferredLangcode());
\Drupal::messenger()
->addMessage(t('You have been subscribed to %newsletter.', [
'%newsletter' => $newsletter->name,
]));
}
}
}
// Process hidden (automatic) subscriptions.
if ($form_state
->getValue('simplenews_hidden')) {
foreach (explode(',', $form_state
->getValue('simplenews_hidden')) as $newsletter_id) {
$subscription_manager
->subscribe($account
->getEmail(), $newsletter_id, FALSE, 'automatically', $account
->getPreferredLangcode());
}
}
}