View source
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
define('WEBFORM_ANTIBOT_NEUTRAL', -1);
define('WEBFORM_ANTIBOT_DISABLED_WEBFORM', 2);
define('WEBFORM_ANTIBOT_ENABLED_WEBFORM', 3);
function _webform_antibot_form(array &$form, FormStateInterface $form_state, $antibot, $antibot_state, $label) {
$t_args = [
'%label' => $label,
':href_antibot' => Url::fromRoute('antibot.settings')
->toString(),
':href_webform' => Url::fromRoute('webform.config')
->toString(),
];
$form['third_party_settings']['antibot'] = [
'#type' => 'details',
'#title' => t('Antibot'),
'#open' => TRUE,
'#description' => t('Prevent SPAM webform submissions from being submitted without JavaScript enabled using the <a href=":href_antibot">antibot</a> method.', $t_args),
];
$form['third_party_settings']['antibot']['antibot'] = [
'#type' => 'checkbox',
'#title' => t('Protect %label with Antibot', $t_args),
'#default_value' => $antibot,
'#return_value' => TRUE,
];
$antibot_state = (int) $antibot_state;
if ($antibot_state !== WEBFORM_ANTIBOT_NEUTRAL) {
$form['third_party_settings']['antibot']['antibot']['#attributes']['disabled'] = 'disabled';
$form_state
->set('antibot_disabled', TRUE);
if ($antibot_state === WEBFORM_ANTIBOT_ENABLED_WEBFORM) {
$form['third_party_settings']['antibot']['antibot']['#default_value'] = 1;
$form['third_party_settings']['antibot']['antibot']['#description'] = t('<a href=":href_webform">Antibot protection</a> is enabled for all webforms.', $t_args);
}
}
$form['#validate'][] = '_webform_antibot_form_validate';
}
function _webform_antibot_form_validate(&$form, FormStateInterface $form_state) {
$third_party_settings = $form_state
->getValue('third_party_settings');
if ($form_state
->get('antibot_disabled') || empty($third_party_settings['antibot']['antibot'])) {
unset($third_party_settings['antibot']['antibot']);
}
$form_state
->setValue('third_party_settings', $third_party_settings);
}
function antibot_webform_admin_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$antibot = $third_party_settings_manager
->getThirdPartySetting('antibot', 'antibot');
$antibot_state = WEBFORM_ANTIBOT_NEUTRAL;
_webform_antibot_form($form, $form_state, $antibot, $antibot_state, t('all webforms'));
}
function antibot_webform_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$webform = $form_state
->getFormObject()
->getEntity();
$antibot = $webform
->getThirdPartySetting('antibot', 'antibot');
if ($third_party_settings_manager
->getThirdPartySetting('antibot', 'antibot')) {
$antibot_state = WEBFORM_ANTIBOT_ENABLED_WEBFORM;
}
else {
$antibot_state = WEBFORM_ANTIBOT_NEUTRAL;
}
_webform_antibot_form($form, $form_state, $antibot, $antibot_state, t('@label webform', [
'@label' => $webform
->label(),
]));
}
function antibot_webform_submission_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_state
->isSubmitted()) {
return;
}
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$webform_submission = $form_state
->getFormObject()
->getEntity();
$webform = $webform_submission
->getWebform();
$antibot = $third_party_settings_manager
->getThirdPartySetting('antibot', 'antibot') ?: $webform
->getThirdPartySetting('antibot', 'antibot');
if ($antibot) {
if (function_exists('antibot_protect_form')) {
$form['#form_id'] = $form_id;
antibot_protect_form($form);
}
else {
$form['#pre_render'][] = 'antibot_form_pre_render';
}
}
}