You are here

function spambot_add_form_protection in Spambot 8

Same name and namespace in other branches
  1. 7 spambot.module \spambot_add_form_protection()

Form builder function to add spambot validations.

Parameters

array $form: Form array on which will be added spambot validation.

array $options: Array of options to be added to form.

1 call to spambot_add_form_protection()
spambot_form_user_register_form_alter in ./spambot.module
Implements hook_form_FORM_ID_alter().

File

./spambot.module, line 279
Main module file.

Code

function spambot_add_form_protection(array &$form, array $options = []) {

  // Don't add any protections if the user can bypass the Spambot.
  if (!\Drupal::currentUser()
    ->hasPermission('protected from spambot scans')) {
    $form['#spambot_validation']['name'] = !empty($options['name']) ? $options['name'] : '';
    $form['#spambot_validation']['mail'] = !empty($options['mail']) ? $options['mail'] : '';
    $form['#spambot_validation']['ip'] = isset($options['ip']) && is_bool($options['ip']) ? $options['ip'] : TRUE;

    // Overriding the ::validateForm() of user registartion form.
    $form['#validate'][] = 'spambot_user_register_form_validate';
  }
}