/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL)
 {
     // Try to load a subscriber from the uid, otherwise just set the mail field
     // on the new subscriber.
     if (isset($user)) {
         $form_state->set('user', $user);
         if ($subscriber = simplenews_subscriber_load_by_uid($user->id())) {
             $this->setEntity($subscriber);
         } else {
             $this->entity->setUserId($user->id());
             $this->entity->setMail($user->getEmail());
         }
     }
     return parent::buildForm($form, $form_state);
 }
 /**
  * {@inheritdoc}
  */
 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);
 }
 /**
  * {@inheritdoc}
  */
 public function build()
 {
     /** @var \Drupal\simplenews\Form\SubscriptionsBlockForm $form_object */
     $form_object = \Drupal::entityManager()->getFormObject('simplenews_subscriber', 'block');
     $form_object->setUniqueId($this->configuration['unique_id']);
     $form_object->setNewsletterIds($this->configuration['newsletters']);
     $form_object->message = $this->configuration['message'];
     // Set the entity on the form.
     if ($user = \Drupal::currentUser()) {
         if ($subscriber = simplenews_subscriber_load_by_uid($user->id())) {
             $form_object->setEntity($subscriber);
         } else {
             $form_object->setEntity(Subscriber::create(array('mail' => $user->getEmail())));
         }
     } else {
         $form_object->setEntity(Subscriber::create());
     }
     return $this->formBuilder->getForm($form_object);
 }