You are here

function word_list_captcha_captcha in CAPTCHA Pack 8

Same name and namespace in other branches
  1. 5 text_captcha/word_list_captcha/word_list_captcha.module \word_list_captcha_captcha()
  2. 6 text_captcha/word_list_captcha/word_list_captcha.module \word_list_captcha_captcha()
  3. 7 text_captcha/word_list_captcha/word_list_captcha.module \word_list_captcha_captcha()

Implements hook_captcha().

File

text_captcha/modules/word_list_captcha/word_list_captcha.module, line 44
Contains general functionality for the module.

Code

function word_list_captcha_captcha($op, $captcha_type = '') {
  $config = \Drupal::config('word_list_captcha.settings');
  switch ($op) {
    case 'list':
      return [
        'Pick the unrelated word',
      ];
    case 'generate':
      if ($captcha_type == 'Pick the unrelated word') {
        $list_size = $config
          ->get('word_list_captcha_list_size');
        list($word_list, $wrong_word) = _word_list_captcha_get_word_list_captcha($list_size);

        // Build options list.
        $options = [];
        foreach ($word_list as $word) {
          $options[$word] = $word;
        }
        $captcha = [];
        $captcha['solution'] = $wrong_word;
        $captcha['form']['captcha_response'] = [
          '#type' => 'radios',
          '#title' => t('Which word does not belong to the list?'),
          '#options' => $options,
          // Extra class needed for additional CSS'ing of the options.
          '#attributes' => [
            'class' => [
              'text-captcha-word-list-radios',
            ],
          ],
          // TODO: the following needs to be ported to Drupal 6, which does not
          // Support DANGEROUS_SKIP_CHECK anymore
          //
          // The following entry '#DANGEROUS_SKIP_CHECK' is needed to prevent
          // that Drupal checks during validation phase if a submitted option
          // is in the list of possible options. (see includes/form.inc)
          // The options are randomly generated on each call and consequently
          // almost never the same during the generate phase and the validation
          // phase.
          //
          '#DANGEROUS_SKIP_CHECK' => TRUE,
          '#required' => TRUE,
          '#cache' => [
            'max-age' => 0,
          ],
        ];
        \Drupal::service('page_cache_kill_switch')
          ->trigger();

        // Add css to form.
        $captcha['form']['captcha_response']['#attached']['library'][] = 'text_captcha/base';
        return $captcha;
      }
  }
}