You are here

function template_preprocess_captcha in CAPTCHA 8

Theme function for a CAPTCHA element.

Render it in a section element if a description of the CAPTCHA is available. Render it as is otherwise.

File

./captcha.module, line 128
This module enables basic CAPTCHA functionality.

Code

function template_preprocess_captcha(&$variables) {
  $element = $variables['element'];
  if (!empty($element['#description']) && isset($element['captcha_widgets'])) {
    $children_keys = Element::children($element);
    $captcha_children_output = '';
    foreach ($children_keys as $key) {
      if (!empty($element[$key])) {
        $captcha_children_output .= \Drupal::service('renderer')
          ->render($element[$key]);
      }
    }
    $variables['details'] = [
      '#type' => 'details',
      '#title' => t('CAPTCHA'),
      '#description' => $element['#description'],
      '#children' => Markup::create($captcha_children_output),
      '#attributes' => [
        'id' => 'captcha',
        'class' => [
          'captcha',
        ],
        'open' => [
          '',
        ],
      ],
      '#open' => TRUE,
    ];
  }
}