You are here

public function Captcha::prepare in Webform 6.x

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

Prepare an element to be rendered within a webform.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission. Webform submission is optional since it is not used by composite sub elements.

Overrides WebformElementBase::prepare

See also

\Drupal\webform\Element\WebformCompositeBase::processWebformComposite

File

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

Class

Captcha
Provides a 'captcha' element.

Namespace

Drupal\webform\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {

  // Hide and solve the element if the user is assigned 'skip CAPTCHA'
  // and '#captcha_admin_mode' is not enabled.
  $is_admin = $this->currentUser
    ->hasPermission('skip CAPTCHA');
  if ($is_admin && empty($element['#captcha_admin_mode'])) {
    $element['#access'] = FALSE;
    $element['#captcha_admin_mode'] = TRUE;
  }

  // Always enable admin mode for test.
  $is_test = strpos($this->routeMatch
    ->getRouteName(), '.webform.test_form') !== FALSE ? TRUE : FALSE;
  if ($is_test) {
    $element['#captcha_admin_mode'] = TRUE;
  }

  // Add default CAPTCHA description if required.
  // @see captcha_form_alter()
  if (empty($element['#description']) && \Drupal::config('captcha.settings')
    ->get('add_captcha_description')) {
    module_load_include('inc', 'captcha');
    $element['#description'] = _captcha_get_description();
  }
  parent::prepare($element, $webform_submission);
  $element['#after_build'][] = [
    get_class($this),
    'afterBuildCaptcha',
  ];
}