You are here

public function Captcha::form in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformElement/Captcha.php \Drupal\webform\Plugin\WebformElement\Captcha::form()

Gets the actual configuration webform array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration webform without any default values.

Overrides WebformElementBase::form

File

src/Plugin/WebformElement/Captcha.php, line 161

Class

Captcha
Provides a 'captcha' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  // Issue #3090624: Call to undefined function trying to add CAPTCHA
  // element to form.
  // @see _captcha_available_challenge_types();
  // @see \Drupal\captcha\Service\CaptchaService::getAvailableChallengeTypes
  $captcha_types = [];
  $captcha_types['default'] = $this
    ->t('Default challenge type');

  // We do our own version of Drupal's module_invoke_all() here because
  // we want to build an array with custom keys and values.
  foreach ($this->moduleHandler
    ->getImplementations('captcha') as $module) {
    $result = call_user_func_array($module . '_captcha', [
      'list',
    ]);
    if (is_array($result)) {
      foreach ($result as $type) {
        $captcha_types["{$module}/{$type}"] = $this
          ->t('@type (from module @module)', [
          '@type' => $type,
          '@module' => $module,
        ]);
      }
    }
  }
  $form['captcha'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('CAPTCHA settings'),
  ];
  $form['captcha']['message'] = [
    '#type' => 'webform_message',
    '#message_type' => 'warning',
    '#message_message' => $this
      ->t('Note that the CAPTCHA module disables page caching of pages that include a CAPTCHA challenge.'),
    '#message_close' => TRUE,
    '#message_storage' => WebformMessageElement::STORAGE_SESSION,
    '#access' => TRUE,
  ];
  $form['captcha']['captcha_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Challenge type'),
    '#required' => TRUE,
    '#options' => $captcha_types,
  ];

  // Custom title and description.
  $form['captcha']['captcha_container'] = [
    '#type' => 'container',
    '#states' => [
      'invisible' => [
        [
          ':input[name="properties[captcha_type]"]' => [
            'value' => 'recaptcha/reCAPTCHA',
          ],
        ],
      ],
    ],
  ];
  $form['captcha']['captcha_container']['captcha_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Question title'),
  ];
  $form['captcha']['captcha_container']['captcha_description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Question description'),
  ];

  // Admin mode.
  $form['captcha']['captcha_admin_mode'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Admin mode'),
    '#description' => $this
      ->t('Presolve the CAPTCHA and always shows it. This is useful for debugging and preview CAPTCHA integration.'),
    '#return_value' => TRUE,
  ];
  return $form;
}