ConfirmMultiForm.php in Simplenews 8
File
src/Form/ConfirmMultiForm.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\SubscriberInterface;
class ConfirmMultiForm extends ConfirmFormBase {
public function getQuestion() {
return t('Confirm subscription');
}
public function getDescription() {
return t('You can always change your subscriptions later.');
}
public function getFormId() {
return 'simplenews_confirm_multi';
}
public function getCancelUrl() {
return new Url('simplenews.newsletter_subscriptions');
}
public function buildForm(array $form, FormStateInterface $form_state, SubscriberInterface $subscriber = NULL) {
$form = parent::buildForm($form, $form_state);
$form['question'] = array(
'#markup' => '<p>' . t('Are you sure you want to confirm the following subscription changes for %user?', array(
'%user' => simplenews_mask_mail($subscriber
->getMail()),
)) . "<p>\n",
);
$subscription_manager = \Drupal::service('simplenews.subscription_manager');
$form['changes'] = array(
'#theme' => 'item_list',
'#items' => $subscription_manager
->getChangesList($subscriber),
);
$form['subscriber'] = array(
'#type' => 'value',
'#value' => $subscriber,
);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$subscriber = $form_state
->getValue('subscriber');
foreach ($subscriber
->getChanges() as $newsletter_id => $action) {
if ($action == 'subscribe') {
if (!$subscriber
->isSubscribed($newsletter_id)) {
$subscriber
->subscribe($newsletter_id);
}
}
elseif ($action == 'unsubscribe') {
if ($subscriber
->isSubscribed($newsletter_id)) {
$subscriber
->unsubscribe($newsletter_id);
}
}
}
$subscriber
->setChanges(array());
$subscriber
->save();
$this
->messenger()
->addMessage(t('Subscription changes confirmed for %user.', array(
'%user' => $subscriber
->getMail(),
)));
$form_state
->setRedirect('<front>');
}
}
Classes
Name |
Description |
ConfirmMultiForm |
Implements a multi confirmation form for simplenews subscriptions. |