You are here

public function SubscriptionsPageForm::buildForm in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/Form/SubscriptionsPageForm.php \Drupal\simplenews\Form\SubscriptionsPageForm::buildForm()
  2. 3.x src/Form/SubscriptionsPageForm.php \Drupal\simplenews\Form\SubscriptionsPageForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/SubscriptionsPageForm.php, line 17

Class

SubscriptionsPageForm
Configure simplenews subscriptions of the logged user.

Namespace

Drupal\simplenews\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $snid = NULL, $timestamp = NULL, $hash = NULL) {
  $user = \Drupal::currentUser();
  if ($subscriber = simplenews_subscriber_load_by_uid($user
    ->id())) {
    $this
      ->setEntity($subscriber);
  }
  elseif ($mail = $user
    ->getEmail()) {
    $this
      ->setEntity(Subscriber::create(array(
      'mail' => $mail,
    )));
  }
  elseif ($snid && $timestamp && $hash) {
    $subscriber = simplenews_subscriber_load($snid);
    if ($subscriber && $hash == simplenews_generate_hash($subscriber
      ->getMail(), 'manage', $timestamp)) {
      $this
        ->setEntity($subscriber);
    }
    else {
      throw new NotFoundHttpException();
    }
  }
  return parent::buildForm($form, $form_state);
}