Skip to content
Snippets Groups Projects
Commit e42d70aa authored by Tim Rohaly's avatar Tim Rohaly
Browse files

Issue #3028981 by TR, PtrTon: Allow custom conditionals for skipping Honeypot validation

parent 360d1345
No related branches found
Tags 5.x-1.8
No related merge requests found
......@@ -5,6 +5,8 @@
* API Functionality for Honeypot module.
*/
use Drupal\Core\Form\FormStateInterface;
/**
* @addtogroup hooks
* @{
......@@ -95,6 +97,26 @@ function hook_honeypot_time_limit($honeypot_time_limit, array $form_values, $num
return $additions;
}
/**
* Determines if honeypot validation should be skipped.
*
* In some cases it can be useful to skip honeypot validation.
* For instance if a certain IP should be allowed to run automated scripts.
*
* @param array $element
* The element triggering the validation
* This will be either the honeypot or time restriction element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The Drupal form state.
*
* @return bool
* FALSE if validation should be skipped, TRUE to proceed as usual.
*/
function hook_honeypot_should_validate(array $element, FormStateInterface $form_state) {
// Skip validation for a certain whitelisted ip.
return \Drupal::request()->getClientIp() === 'some-whitelisted-ip';
}
/**
* @} End of "addtogroup hooks".
*/
......@@ -296,6 +296,11 @@ class HoneypotService implements HoneypotServiceInterface {
* The complete form structure.
*/
public function validateHoneypot(array &$element, FormStateInterface $form_state, array &$complete_form): void {
$shouldValidate = $this->moduleHandler->invokeAll('honeypot_should_validate', [$element, $form_state]);
if (in_array(FALSE, $shouldValidate, TRUE)) {
return;
}
// Get the honeypot field value.
$honeypot_value = $element['#value'];
......@@ -323,6 +328,11 @@ class HoneypotService implements HoneypotServiceInterface {
return;
}
$shouldValidate = $this->moduleHandler->invokeAll('honeypot_should_validate', [$element, $form_state]);
if (in_array(FALSE, $shouldValidate, TRUE)) {
return;
}
$triggering_element = $form_state->getTriggeringElement();
// Don't do anything if the triggering element is a preview button.
if ($triggering_element['#value'] == (string) $this->t('Preview')) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment