You are here

function image_captcha_after_build_process in CAPTCHA 8

Add image refresh button to captcha form element.

Return value

array The processed element.

See also

captcha_element_info()

image_captcha_element_info_alter()

1 string reference to 'image_captcha_after_build_process'
image_captcha_element_info_alter in image_captcha/image_captcha.module
Implements hook_element_info_alter().

File

image_captcha/image_captcha.module, line 366
Implements image CAPTCHA for use with the CAPTCHA module.

Code

function image_captcha_after_build_process($element) {
  $form_id = $element['#captcha_info']['form_id'];
  $captcha_point = captcha_get_form_id_setting($form_id);
  $is_image_captcha = FALSE;
  if (isset($captcha_point->captchaType) && $captcha_point->captchaType == 'image_captcha/Image') {
    $is_image_captcha = TRUE;
  }
  elseif (isset($captcha_point->captchaType) && $captcha_point->captchaType == 'default') {
    $default_challenge = \Drupal::service('config.manager')
      ->getConfigFactory()
      ->get('captcha.settings')
      ->get('default_challenge');
    if ($default_challenge == 'image_captcha/Image') {
      $is_image_captcha = TRUE;
    }
  }
  if ($is_image_captcha && isset($element['captcha_widgets']['captcha_image'])) {
    $uri = Link::fromTextAndUrl(t('Get new captcha!'), new Url('image_captcha.refresh', [
      'form_id' => $form_id,
    ], [
      'attributes' => [
        'class' => [
          'reload-captcha',
        ],
      ],
    ]));
    $element['captcha_widgets']['captcha_refresh'] = [
      '#theme' => 'image_captcha_refresh',
      '#captcha_refresh_link' => $uri,
    ];
  }
  return $element;
}