SubscriberValidateForm.php in Simplenews 3.x
File
src/Form/SubscriberValidateForm.php
View source
<?php
namespace Drupal\simplenews\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Url;
use Drupal\simplenews\Entity\Subscriber;
use Drupal\simplenews\Mail\MailerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class SubscriberValidateForm extends FormBase {
protected $mailManager;
protected $mailer;
public function __construct(MailManagerInterface $mail_manager, MailerInterface $mailer) {
$this->mailManager = $mail_manager;
$this->mailer = $mailer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.mail'), $container
->get('simplenews.mailer'));
}
public function getFormId() {
return 'simplenews_subscriber_validate';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$user = $this
->currentUser();
if ($user
->isAuthenticated()) {
return new RedirectResponse(Url::fromRoute('simplenews.newsletter_subscriptions_user', [
'user' => $user
->id(),
])
->toString());
}
$form['explain'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('Request an email with a link to manage your subscriptions.'),
'#suffix' => '</p>',
];
$form['mail'] = [
'#type' => 'email',
'#title' => $this
->t('Email'),
'#description' => $this
->t('Subscribed email address'),
'#required' => TRUE,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$mail = trim($form_state
->getValue('mail'));
if (($subscriber = Subscriber::loadByMail($mail)) && $subscriber
->getStatus()) {
$params['from'] = $this->mailer
->getFrom();
$params['context']['simplenews_subscriber'] = $subscriber;
$this->mailManager
->mail('simplenews', 'validate', $subscriber
->getMail(), $subscriber
->getLangcode(), $params, $params['from']['address']);
}
$this
->messenger()
->addStatus($this
->t('If %mail is subscribed, an email will be sent with a link to manage your subscriptions.', [
'%mail' => $mail,
]));
$form_state
->setRedirect('<front>');
}
}