View source
<?php
namespace Drupal\disclaimer\Form;
use Drupal\block\Entity\Block;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DisclaimerEmailMatchForm extends FormBase {
public $blockSettings = [];
public function __construct(RendererInterface $renderer) {
$this->renderer = $renderer;
}
public function getFormId() {
return 'disclaimer_email_match_form';
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$ajax_wrapper_id = Html::getUniqueId('box-container');
$form['#prefix'] = '<div id="' . $ajax_wrapper_id . '">';
$form['#suffix'] = '</div>';
$form['email'] = [
'#type' => 'email',
'#title' => $this
->t('E-mail'),
'#weight' => 10,
];
$form['block_id'] = [
'#type' => 'hidden',
'#default_value' => '',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#weight' => 20,
'#ajax' => [
'callback' => '::ajaxSubmit',
'wrapper' => $ajax_wrapper_id,
],
];
$form['leave'] = [
'#type' => 'button',
'#name' => 'leave',
'#value' => $this
->t('Leave'),
'#weight' => 30,
'#ajax' => [
'callback' => '::leaveForm',
'wrapper' => $ajax_wrapper_id,
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if ($form_state
->getTriggeringElement()['#name'] == 'leave') {
return;
}
parent::validateForm($form, $form_state);
$this
->populateSettings($form, $form_state);
if (empty($form_state
->getValue('block_id')) || empty($this->blockSettings['email_validation_fail'])) {
$form_state
->setErrorByName('', $this
->t('Unable to verify the e-mail.'));
$this
->logger('disclaimer')
->error('disclaimer_email_match_form needs to be provided with block_id');
return;
}
if (empty($form_state
->getValue('email'))) {
$form_state
->setErrorByName('email', $this
->t('E-mail field is required.'));
}
if (!$this
->validateEmail($form, $form_state)) {
$form_state
->setErrorByName('email', $this->blockSettings['email_validation_fail']);
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
setcookie('disclaimer_email_' . Html::escape($this->blockSettings['machine_name']), 1, time() + $this->blockSettings['max_age']);
}
public function leaveForm(array &$form, FormStateInterface $form_state) {
$this
->populateSettings($form, $form_state);
$response = new AjaxResponse();
$response
->addCommand(new RedirectCommand($this->blockSettings['redirect']));
return $response;
}
public function ajaxSubmit(array &$form, FormStateInterface $form_state) {
$status_messages = [
'#type' => 'status_messages',
];
if ($form_state
->hasAnyErrors()) {
$form['#prefix'] .= $this->renderer
->renderRoot($status_messages);
return $form;
}
else {
$response = new AjaxResponse();
$response
->addCommand(new InvokeCommand('.disclaimer_email__challenge', 'dialog', [
'close',
]));
return $response;
}
}
private function validateEmail(array $form, FormStateInterface $form_state) {
$this
->populateSettings($form, $form_state);
if (!isset($this->blockSettings['allowed_emails'])) {
return FALSE;
}
$allowed_emails = array_map('trim', explode("\n", $this->blockSettings['allowed_emails']));
$provided_email = $form_state
->getValue('email');
foreach ($allowed_emails as $allowed_email) {
if (fnmatch($allowed_email, $provided_email, FNM_CASEFOLD)) {
return TRUE;
}
}
return FALSE;
}
private function populateSettings(array $form, FormStateInterface $form_state) {
$block = Block::load($form_state
->getValue('block_id'));
if ($block) {
$this->blockSettings = $block
->get('settings');
}
}
}