View source
<?php
namespace Drupal\simplenews\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\simplenews\Entity\Subscriber;
class SubscriptionsBlockForm extends SubscriptionsFormBase {
protected $uniqueId;
public $message;
public function getFormId() {
if (empty($this->uniqueId)) {
throw new \Exception('Unique ID must be set with setUniqueId.');
}
return 'simplenews_subscriptions_block_' . $this->uniqueId;
}
public function setUniqueId($id) {
$this->uniqueId = $id;
}
public function form(array $form, FormStateInterface $form_state) {
if (count($this
->getNewsletters()) == 1) {
$this
->getSubscriptionWidget($form_state)
->setHidden();
}
$form = parent::form($form, $form_state);
$form['subscriptions']['widget']['#title'] = $this
->t('Manage your newsletter subscriptions');
$form['subscriptions']['widget']['#description'] = $this
->t('Select the newsletter(s) to which you want to subscribe.');
if ($this->message) {
$form['message'] = [
'#type' => 'item',
'#markup' => $this->message,
];
}
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Subscribe');
$user = \Drupal::currentUser();
$link = $user
->isAuthenticated() ? Url::fromRoute('simplenews.newsletter_subscriptions_user', [
'user' => $user
->id(),
]) : Url::fromRoute('simplenews.newsletter_validate');
$actions['manage'] = [
'#title' => $this
->t('Manage existing'),
'#type' => 'link',
'#url' => $link,
];
return $actions;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$mail = $form_state
->getValue([
'mail',
0,
'value',
]);
if ($this->entity
->isNew() && ($subscriber = Subscriber::loadByMail($mail))) {
$this
->setEntity($subscriber);
}
parent::validateForm($form, $form_state);
if (!$this
->getSubscriptionWidget($form_state)
->isHidden() && !count($form_state
->getValue('subscriptions'))) {
$form_state
->setErrorByName('subscriptions', $this
->t('You must select at least one newsletter.'));
}
$mail = $form_state
->getValue([
'mail',
0,
'value',
]);
if (($user = user_load_by_mail($mail)) && $user
->isBlocked()) {
$message = $this
->t('The email address %mail belongs to a blocked user.', [
'%mail' => $mail,
]);
$form_state
->setErrorByName('mail', $message);
}
}
public function submitExtra(array $form, FormStateInterface $form_state) {
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
foreach ($this
->extractNewsletterIds($form_state, TRUE) as $newsletter_id) {
$subscription_manager
->subscribe($this->entity
->getMail(), $newsletter_id, NULL, 'website');
}
$sent = $subscription_manager
->sendConfirmations();
$this
->messenger()
->addMessage($this
->getSubmitMessage($form_state, $sent));
}
protected function getSubmitMessage(FormStateInterface $form_state, $confirm) {
if ($confirm) {
return $this
->t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.');
}
return $this
->t('You have been subscribed.');
}
}