You are here

public function SubscriberForm::form in Simplenews 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/SubscriberForm.php \Drupal\simplenews\Form\SubscriberForm::form()
  2. 8 src/Form/SubscriberForm.php \Drupal\simplenews\Form\SubscriberForm::form()

Gets the actual form array to be built.

Overrides SubscriptionsFormBase::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/SubscriberForm.php, line 23

Class

SubscriberForm
Form controller for the subscriber edit forms.

Namespace

Drupal\simplenews\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /** @var \Drupal\simplenews\SubscriberInterface $subscriber */
  $subscriber = $this->entity;
  if ($mail = $subscriber
    ->getMail()) {
    $form['#title'] = $this
      ->t('Edit subscriber @mail', [
      '@mail' => $mail,
    ]);
  }
  $form['activated'] = [
    '#title' => $this
      ->t('Status'),
    '#type' => 'fieldset',
    '#description' => $this
      ->t('Whether the subscription is active or blocked.'),
    '#weight' => 15,
  ];
  $form['activated']['status'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Active'),
    '#default_value' => $subscriber
      ->getStatus(),
    '#disabled' => !$subscriber
      ->get('status')
      ->access('edit'),
  ];
  $language_manager = \Drupal::languageManager();
  if ($language_manager
    ->isMultilingual()) {
    $languages = $language_manager
      ->getLanguages();
    foreach ($languages as $langcode => $language) {
      $language_options[$langcode] = $language
        ->getName();
    }
    $form['language'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Preferred language'),
      '#description' => $this
        ->t('The e-mails will be localized in language chosen. Real users have their preference in account settings.'),
      '#disabled' => FALSE,
    ];
    if ($subscriber
      ->getUserId()) {

      // Fallback if user has not defined a language.
      $form['language']['langcode'] = [
        '#type' => 'item',
        '#title' => $this
          ->t('User language'),
        '#markup' => $subscriber
          ->language()
          ->getName(),
      ];
    }
    else {
      $form['language']['langcode'] = [
        '#type' => 'select',
        '#default_value' => $subscriber
          ->language()
          ->getId(),
        '#options' => $language_options,
        '#required' => TRUE,
      ];
    }
  }
  return $form;
}