ConfirmAddForm.php in Simplenews 8
File
src/Form/ConfirmAddForm.php
View source
<?php
namespace Drupal\simplenews\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\simplenews\NewsletterInterface;
class ConfirmAddForm extends ConfirmFormBase {
public function getQuestion() {
return t('Confirm subscription');
}
public function getConfirmText() {
return t('Subscribe');
}
public function getDescription() {
return t('You can always unsubscribe later.');
}
public function getFormId() {
return 'simplenews_confirm_add';
}
public function getCancelUrl() {
return new Url('simplenews.newsletter_subscriptions');
}
public function buildForm(array $form, FormStateInterface $form_state, $mail = '', NewsletterInterface $newsletter = NULL) {
$form = parent::buildForm($form, $form_state);
$form['question'] = array(
'#markup' => '<p>' . t('Are you sure you want to add %user to the %newsletter mailing list?', array(
'%user' => simplenews_mask_mail($mail),
'%newsletter' => $newsletter->name,
)) . "<p>\n",
);
$form['mail'] = array(
'#type' => 'value',
'#value' => $mail,
);
$form['newsletter'] = array(
'#type' => 'value',
'#value' => $newsletter,
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
\Drupal::service('simplenews.subscription_manager')
->subscribe($form_state
->getValue('mail'), $form_state
->getValue('newsletter')
->id(), FALSE, 'website');
$config = \Drupal::config('simplenews.settings');
if ($path = $config
->get('subscription.confirm_subscribe_page')) {
$form_state
->setRedirectUrl(Url::fromUri("internal:{$path}"));
}
else {
$this
->messenger()
->addMessage(t('%user was added to the %newsletter mailing list.', array(
'%user' => $form_state
->getValue('mail'),
'%newsletter' => $form_state
->getValue('newsletter')->name,
)));
$form_state
->setRedirect('<front>');
}
}
}
Classes
Name |
Description |
ConfirmAddForm |
Implements a add confirmation form for simplenews subscriptions. |