You are here

function simplenews_form_user_register_form_alter in Simplenews 8.2

Same name and namespace in other branches
  1. 8 simplenews.module \simplenews_form_user_register_form_alter()
  2. 7.2 simplenews.module \simplenews_form_user_register_form_alter()
  3. 7 simplenews.module \simplenews_form_user_register_form_alter()
  4. 3.x simplenews.module \simplenews_form_user_register_form_alter()

Implements hook_form_FORM_ID_alter().

Add simplenews subscription fields to user register form.

@todo mode this function to another place in the module.

File

./simplenews.module, line 350
Simplenews node handling, sent email, newsletter block and general hooks.

Code

function simplenews_form_user_register_form_alter(&$form, FormStateInterface $form_state) {
  $options = $default_value = $hidden = [];

  // Determine the lists to which a user can choose to subscribe.
  // Determine to which other list a user is automatically subscribed.
  foreach (simplenews_newsletter_get_all() as $newsletter) {
    $subscribe_new_account = $newsletter->new_account;
    $opt_inout_method = $newsletter->opt_inout;
    if (($subscribe_new_account == 'on' || $subscribe_new_account == 'off') && ($opt_inout_method == 'single' || $opt_inout_method == 'double')) {
      $options[$newsletter
        ->id()] = $newsletter->name;
      if ($subscribe_new_account == 'on') {
        $default_value[] = $newsletter
          ->id();
      }
    }
    else {
      if ($subscribe_new_account == 'silent' || $subscribe_new_account == 'on' && $opt_inout_method == SIMPLENEWS_OPT_INOUT_HIDDEN) {
        $hidden[] = $newsletter
          ->id();
      }
    }
  }
  if (count($options)) {

    // @todo Change this text: use less words;
    $form['simplenews'] = [
      '#type' => 'fieldset',
      '#description' => t('Select the newsletter(s) to which you wish to subscribe.'),
    ];
    $form['simplenews']['subscriptions'] = [
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $default_value,
    ];
  }
  if (count($hidden)) {
    $form['simplenews_hidden'] = [
      '#type' => 'hidden',
      '#value' => implode(',', $hidden),
    ];
  }
  $form['actions']['submit']['#submit'][] = 'simplenews_user_profile_form_submit';
}