function phrase_captcha_captcha in CAPTCHA Pack 8
Same name and namespace in other branches
- 5 text_captcha/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()
- 6 text_captcha/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()
- 7 text_captcha/phrase_captcha/phrase_captcha.module \phrase_captcha_captcha()
Implements hook_captcha().
File
- text_captcha/
modules/ phrase_captcha/ phrase_captcha.module, line 27 - Implementation of a phrase based CAPTCHA, for use with the CAPTCHA module.
Code
function phrase_captcha_captcha($op, $captcha_type = '') {
$config = \Drupal::config('phrase_captcha.settings');
switch ($op) {
case 'list':
return [
'Phrase CAPTCHA',
];
case 'generate':
if ($captcha_type == 'Phrase CAPTCHA') {
// Generate words.
$words = _phrase_captcha_generate_words($config
->get('phrase_captcha_word_quantity'));
// Pick a random word selection challenge.
$word_challenges = _phrase_captcha_enabled_word_challenges();
$key = array_rand($word_challenges);
$function = $word_challenges[$key];
list($phrase_words, $question, $solution) = call_user_func($function, $words);
// Build options list.
$all_words = array_merge($words, _phrase_captcha_generate_words($config
->get('phrase_captcha_additional_word_quantity')));
shuffle($all_words);
$options = [];
foreach ($all_words as $word) {
$options[$word] = $word;
}
// Store the answer and build the form elements.
$captcha = [];
$captcha['solution'] = $solution;
$captcha['form']['captcha_phrase'] = [
'#type' => 'markup',
'#value' => '"' . implode(' ', $phrase_words) . '"',
'#weight' => -2,
];
$captcha['form']['captcha_response'] = [
'#type' => 'radios',
'#title' => $question,
'#options' => $options,
// Extra class needed for additional CSS'ing of the options.
'#attributes' => [
'class' => [
'text-captcha-word-list-radios',
],
],
// 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;
}
}
}