You are here

foo_captcha.module in CAPTCHA Pack 8

Contains general functionality of the module.

File

foo_captcha/foo_captcha.module
View source
<?php

/**
 * @file
 * Contains general functionality of the module.
 */

/**
 * Implements hook_help().
 */
function foo_captcha_help($path, $arg) {
  switch ($path) {
    case 'admin/config/people/captcha/foo_captcha':
      return '<p>' . t('This is a very simple CAPTCHA, which requires users to enter "foo" in a textfield.') . '</p>';
  }
}

/**
 * Implements hook_captcha().
 */
function foo_captcha_captcha($op, $captcha_type = '') {
  switch ($op) {
    case 'list':
      return [
        'Foo CAPTCHA',
      ];
    case 'generate':
      if ($captcha_type == 'Foo CAPTCHA') {
        $captcha = [];
        $captcha['solution'] = 'foo';
        $captcha['form']['captcha_response'] = [
          '#type' => 'textfield',
          '#title' => t('Enter "foo"'),
          '#required' => TRUE,
          '#process' => [
            'foo_captcha_process',
          ],
          '#cache' => [
            'max-age' => 0,
          ],
        ];
        \Drupal::service('page_cache_kill_switch')
          ->trigger();
        return $captcha;
      }
      break;
  }
}

/**
 * Process the response to the foo CAPTCHA before validation.
 */
function foo_captcha_process($element, $edit) {
  if (\Drupal::config('foo_captcha.settings')
    ->get('foo_captcha_ignore_spaces')) {
    $element['#value'] = preg_replace('/\\s*/', '', $element['#value']);
  }
  return $element;
}

Functions

Namesort descending Description
foo_captcha_captcha Implements hook_captcha().
foo_captcha_help Implements hook_help().
foo_captcha_process Process the response to the foo CAPTCHA before validation.