View source
<?php
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
define('WEBFORM_HONEYPOT_NEUTRAL', -1);
define('WEBFORM_HONEYPOT_DISABLED_ALL', 0);
define('WEBFORM_HONEYPOT_ENABLED_ALL', 1);
define('WEBFORM_HONEYPOT_DISABLED_WEBFORM', 2);
define('WEBFORM_HONEYPOT_ENABLED_WEBFORM', 3);
function _webform_honeypot_form(array &$form, FormStateInterface $form_state, $honeypot, $honeypot_state, $time_restriction, $time_restriction_state, $label) {
$t_args = [
'%label' => $label,
':href_honeypot' => Url::fromRoute('honeypot.config')
->toString(),
':href_webform' => Url::fromRoute('webform.config')
->toString(),
];
$form['third_party_settings']['honeypot'] = [
'#type' => 'details',
'#title' => t('Honeypot'),
'#open' => TRUE,
'#description' => t('Mitigate SPAM webform submissions using the <a href=":href_honeypot">honeypot</a> method.', $t_args),
];
$form['third_party_settings']['honeypot']['honeypot'] = [
'#type' => 'checkbox',
'#title' => t('Protect %label with Honeypot', $t_args),
'#default_value' => $honeypot,
'#return_value' => TRUE,
];
if ($honeypot_state !== WEBFORM_HONEYPOT_NEUTRAL) {
$form['third_party_settings']['honeypot']['honeypot']['#attributes']['disabled'] = 'disabled';
$form_state
->set('honeypot_disabled', TRUE);
if ($honeypot_state === WEBFORM_HONEYPOT_ENABLED_ALL) {
$form['third_party_settings']['honeypot']['honeypot']['#default_value'] = 1;
$form['third_party_settings']['honeypot']['honeypot']['#description'] = t('<a href=":href_honeypot">Honeypot protection</a> is enabled for all webforms.', $t_args);
}
elseif ($honeypot_state === WEBFORM_HONEYPOT_ENABLED_WEBFORM) {
$form['third_party_settings']['honeypot']['honeypot']['#default_value'] = 1;
$form['third_party_settings']['honeypot']['honeypot']['#description'] = t('<a href=":href_webform">Honeypot protection</a> is enabled for all webforms.', $t_args);
}
}
$form['third_party_settings']['honeypot']['time_restriction'] = [
'#type' => 'checkbox',
'#title' => t('Add time restriction to %label', $t_args),
'#description' => '',
'#default_value' => $time_restriction,
'#return_value' => TRUE,
];
if ($time_restriction_state !== WEBFORM_HONEYPOT_NEUTRAL) {
$form['third_party_settings']['honeypot']['time_restriction']['#attributes']['disabled'] = 'disabled';
$form_state
->set('time_restriction_disabled', TRUE);
if ($time_restriction_state === WEBFORM_HONEYPOT_DISABLED_ALL) {
$form['third_party_settings']['honeypot']['time_restriction']['#default_value'] = 0;
$form['third_party_settings']['honeypot']['time_restriction']['#description'] = t('<a href=":href_honeypot">Time limit</a> is disabled for all webforms.', $t_args);
}
elseif ($time_restriction_state === WEBFORM_HONEYPOT_ENABLED_WEBFORM) {
$form['third_party_settings']['honeypot']['time_restriction']['#default_value'] = 1;
$form['third_party_settings']['honeypot']['time_restriction']['#description'] = t('<a href=":href_webform">Time limit</a> is enabled for all webforms.', $t_args);
}
}
$form['third_party_settings']['honeypot']['time_restriction']['#description'] = ($form['third_party_settings']['honeypot']['time_restriction']['#description'] ? '<br/>' : '') . '<strong>' . t('Page caching will be disabled when a time restriction is applied to a webform.') . '</strong>';
$form['#validate'][] = '_webform_honeypot_form_validate';
}
function _webform_honeypot_form_validate(&$form, FormStateInterface $form_state) {
$third_party_settings = $form_state
->getValue('third_party_settings');
if ($form_state
->get('honeypot_disabled') || empty($third_party_settings['honeypot']['honeypot'])) {
unset($third_party_settings['honeypot']['honeypot']);
}
if ($form_state
->get('time_restriction_disabled') || empty($third_party_settings['honeypot']['time_restriction'])) {
unset($third_party_settings['honeypot']['time_restriction']);
}
$form_state
->setValue('third_party_settings', $third_party_settings);
}
function honeypot_webform_admin_third_party_settings_form_alter(&$form, FormStateInterface $form_state) {
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$honeypot = (int) $third_party_settings_manager
->getThirdPartySetting('honeypot', 'honeypot');
$honeypot_state = \Drupal::config('honeypot.settings')
->get('protect_all_forms') ? WEBFORM_HONEYPOT_ENABLED_ALL : WEBFORM_HONEYPOT_NEUTRAL;
$honeypot_time_limit = (int) \Drupal::config('honeypot.settings')
->get('time_limit');
$time_restriction = (int) $third_party_settings_manager
->getThirdPartySetting('honeypot', 'time_restriction');
$time_restriction_state = $honeypot_time_limit === 0 ? WEBFORM_HONEYPOT_DISABLED_ALL : WEBFORM_HONEYPOT_NEUTRAL;
_webform_honeypot_form($form, $form_state, $honeypot, $honeypot_state, $time_restriction, $time_restriction_state, t('all webforms'));
}
function honeypot_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();
$honeypot = (int) $webform
->getThirdPartySetting('honeypot', 'honeypot');
if (\Drupal::config('honeypot.settings')
->get('protect_all_forms')) {
$honeypot_state = WEBFORM_HONEYPOT_ENABLED_ALL;
}
elseif ($third_party_settings_manager
->getThirdPartySetting('honeypot', 'honeypot')) {
$honeypot_state = WEBFORM_HONEYPOT_ENABLED_WEBFORM;
}
else {
$honeypot_state = WEBFORM_HONEYPOT_NEUTRAL;
}
$time_restriction = (int) $webform
->getThirdPartySetting('honeypot', 'time_restriction');
$honeypot_time_limit = (int) \Drupal::config('honeypot.settings')
->get('time_limit');
if ($honeypot_time_limit === 0) {
$time_restriction_state = WEBFORM_HONEYPOT_DISABLED_ALL;
}
elseif ($third_party_settings_manager
->getThirdPartySetting('honeypot', 'time_restriction')) {
$time_restriction_state = WEBFORM_HONEYPOT_ENABLED_WEBFORM;
}
else {
$time_restriction_state = WEBFORM_HONEYPOT_NEUTRAL;
}
_webform_honeypot_form($form, $form_state, $honeypot, $honeypot_state, $time_restriction, $time_restriction_state, t('@label webform', [
'@label' => $webform
->label(),
]));
}
function honeypot_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();
$options = [];
$honeypot = (int) $third_party_settings_manager
->getThirdPartySetting('honeypot', 'honeypot') ?: $webform
->getThirdPartySetting('honeypot', 'honeypot');
if ($honeypot) {
$options[] = 'honeypot';
}
$time_restriction = (int) $third_party_settings_manager
->getThirdPartySetting('honeypot', 'time_restriction') ?: $webform
->getThirdPartySetting('honeypot', 'time_restriction');
if ($time_restriction) {
$options[] = 'time_restriction';
}
if ($options) {
honeypot_add_form_protection($form, $form_state, $options);
}
}