Skip to content
Snippets Groups Projects
Commit 87102c44 authored by Aurelien Navarre's avatar Aurelien Navarre Committed by Jeff Geerling
Browse files

Issue #2330259 by anavarre: Fixed Follow-up to Update honeypot module to use form state interface.

parent e050fb42
No related branches found
No related tags found
No related merge requests found
File moved
......@@ -5,21 +5,7 @@
* Honeypot module, for deterring spam bots from completing Drupal forms.
*/
/**
* Implements hook_permission().
*/
function honeypot_permission() {
return array(
'administer honeypot' => array(
'title' => t('Administer Honeypot'),
'description' => t('Administer Honeypot-protected forms and settings'),
),
'bypass honeypot protection' => array(
'title' => t('Bypass Honeypot protection'),
'description' => t('Bypass Honeypot form protection.'),
),
);
}
use Drupal\Core\Form\FormStateInterface;
/**
* Implements of hook_cron().
......@@ -37,7 +23,7 @@ function honeypot_cron() {
*
* Add Honeypot features to forms enabled in the Honeypot admin interface.
*/
function honeypot_form_alter(&$form, &$form_state, $form_id) {
function honeypot_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Don't use for maintenance mode forms (install, update, etc.).
if (defined('MAINTENANCE_MODE')) {
return;
......@@ -111,7 +97,7 @@ function honeypot_get_protected_forms() {
* Array of options to be added to form. Currently accepts 'honeypot' and
* 'time_restriction'.
*/
function honeypot_add_form_protection(&$form, &$form_state, $options = array()) {
function honeypot_add_form_protection(&$form, FormStateInterface $form_state, $options = array()) {
$account = \Drupal::currentUser();
// Allow other modules to alter the protections applied to this form.
......@@ -175,21 +161,21 @@ function honeypot_add_form_protection(&$form, &$form_state, $options = array())
/**
* Validate honeypot field.
*/
function _honeypot_honeypot_validate($element, &$form_state) {
function _honeypot_honeypot_validate($element, FormStateInterface $form_state) {
// Get the honeypot field value.
$honeypot_value = $element['#value'];
// Make sure it's empty.
if (!empty($honeypot_value)) {
_honeypot_log($form_state['values']['form_id'], 'honeypot');
form_set_error('', $form_state, t('There was a problem with your form submission. Please refresh the page and try again.'));
$form_state->setError('', $form_state, t('There was a problem with your form submission. Please refresh the page and try again.'));
}
}
/**
* Validate honeypot's time restriction field.
*/
function _honeypot_time_restriction_validate($element, &$form_state) {
function _honeypot_time_restriction_validate($element, FormStateInterface $form_state) {
// Don't do anything if the triggering element is a preview button.
if ($form_state['triggering_element']['#value'] == t('Preview')) {
return;
......@@ -207,7 +193,7 @@ function _honeypot_time_restriction_validate($element, &$form_state) {
_honeypot_log($form_state['values']['form_id'], 'honeypot_time');
$time_limit = honeypot_get_time_limit();
$form_state['values']['honeypot_time'] = time();
form_set_error('', $form_state, t('There was a problem with your form submission. Please wait @limit seconds and try again.', array('@limit' => $time_limit)));
$form_state->setError('', $form_state, t('There was a problem with your form submission. Please wait @limit seconds and try again.', array('@limit' => $time_limit)));
}
}
......
'administer honeypot':
title: 'Administer Honeypot'
description: 'Administer Honeypot-protected forms and settings.'
'bypass honeypot protection':
title: 'Bypass Honeypot protection'
description: 'Bypass Honeypot form protection.'
diff --git a/honeypot.links.menu.yml b/honeypot.links.menu.yml
new file mode 100644
index 0000000..2a7ec41
--- /dev/null
+++ b/honeypot.links.menu.yml
@@ -0,0 +1,5 @@
+honeypot.config:
+ title: Honeypot configuration
+ description: 'Configure Honeypot spam prevention and the forms on which Honeypot will be used.'
+ parent: system.admin_config_content
+ route_name: honeypot.config
diff --git a/honeypot.menu_links.yml b/honeypot.menu_links.yml
deleted file mode 100644
index 2a7ec41..0000000
--- a/honeypot.menu_links.yml
+++ /dev/null
@@ -1,5 +0,0 @@
-honeypot.config:
- title: Honeypot configuration
- description: 'Configure Honeypot spam prevention and the forms on which Honeypot will be used.'
- parent: system.admin_config_content
- route_name: honeypot.config
diff --git a/honeypot.module b/honeypot.module
index 177704f..75bb2dd 100644
--- a/honeypot.module
+++ b/honeypot.module
@@ -5,21 +5,7 @@
* Honeypot module, for deterring spam bots from completing Drupal forms.
*/
-/**
- * Implements hook_permission().
- */
-function honeypot_permission() {
- return array(
- 'administer honeypot' => array(
- 'title' => t('Administer Honeypot'),
- 'description' => t('Administer Honeypot-protected forms and settings'),
- ),
- 'bypass honeypot protection' => array(
- 'title' => t('Bypass Honeypot protection'),
- 'description' => t('Bypass Honeypot form protection.'),
- ),
- );
-}
+use Drupal\Core\Form\FormStateInterface;
/**
* Implements of hook_cron().
@@ -37,7 +23,7 @@ function honeypot_cron() {
*
* Add Honeypot features to forms enabled in the Honeypot admin interface.
*/
-function honeypot_form_alter(&$form, &$form_state, $form_id) {
+function honeypot_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Don't use for maintenance mode forms (install, update, etc.).
if (defined('MAINTENANCE_MODE')) {
return;
@@ -111,7 +97,7 @@ function honeypot_get_protected_forms() {
* Array of options to be added to form. Currently accepts 'honeypot' and
* 'time_restriction'.
*/
-function honeypot_add_form_protection(&$form, &$form_state, $options = array()) {
+function honeypot_add_form_protection(&$form, FormStateInterface $form_state, $options = array()) {
$account = \Drupal::currentUser();
// Allow other modules to alter the protections applied to this form.
@@ -175,21 +161,21 @@ function honeypot_add_form_protection(&$form, &$form_state, $options = array())
/**
* Validate honeypot field.
*/
-function _honeypot_honeypot_validate($element, &$form_state) {
+function _honeypot_honeypot_validate($element, FormStateInterface $form_state) {
// Get the honeypot field value.
$honeypot_value = $element['#value'];
// Make sure it's empty.
if (!empty($honeypot_value)) {
_honeypot_log($form_state['values']['form_id'], 'honeypot');
- form_set_error('', $form_state, t('There was a problem with your form submission. Please refresh the page and try again.'));
+ $form_state->setError('', $form_state, t('There was a problem with your form submission. Please refresh the page and try again.'));
}
}
/**
* Validate honeypot's time restriction field.
*/
-function _honeypot_time_restriction_validate($element, &$form_state) {
+function _honeypot_time_restriction_validate($element, FormStateInterface $form_state) {
// Don't do anything if the triggering element is a preview button.
if ($form_state['triggering_element']['#value'] == t('Preview')) {
return;
@@ -207,7 +193,7 @@ function _honeypot_time_restriction_validate($element, &$form_state) {
_honeypot_log($form_state['values']['form_id'], 'honeypot_time');
$time_limit = honeypot_get_time_limit();
$form_state['values']['honeypot_time'] = time();
- form_set_error('', $form_state, t('There was a problem with your form submission. Please wait @limit seconds and try again.', array('@limit' => $time_limit)));
+ $form_state->setError('', $form_state, t('There was a problem with your form submission. Please wait @limit seconds and try again.', array('@limit' => $time_limit)));
}
}
diff --git a/honeypot.permissions.yml b/honeypot.permissions.yml
new file mode 100644
index 0000000..3db0d8d
--- /dev/null
+++ b/honeypot.permissions.yml
@@ -0,0 +1,7 @@
+'administer honeypot':
+ title: 'Administer Honeypot'
+ description: 'Administer Honeypot-protected forms and settings.'
+
+'bypass honeypot protection':
+ title: 'Bypass Honeypot protection'
+ description: 'Bypass Honeypot form protection.'
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