function simplenews_user_profile_form_submit in Simplenews 8
Same name and namespace in other branches
- 8.2 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 simplenews 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 387 - 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) {
$newsletters = simplenews_newsletter_load_multiple($ids);
foreach ($newsletters as $newsletter) {
$subscription_manager
->subscribe($account
->getEmail(), $newsletter
->id(), FALSE, 'website', $account
->getPreferredLangcode());
\Drupal::messenger()
->addMessage(t('You have been subscribed to %newsletter.', array(
'%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());
}
}
}