You are here

function _captcha_webform_submission_form_after_build in Webform 6.x

Same name and namespace in other branches
  1. 8.5 third_party_settings/webform.captcha.inc \_captcha_webform_submission_form_after_build()

After build callback to add warning to CAPTCHA placement.

1 string reference to '_captcha_webform_submission_form_after_build'
captcha_webform_submission_form_alter in third_party_settings/webform.captcha.inc
Implements hook_webform_submission_form_alter().

File

third_party_settings/webform.captcha.inc, line 84
Integrates third party settings on the CAPTCHA module's behalf.

Code

function _captcha_webform_submission_form_after_build(array $form, FormStateInterface $form_state) {

  // Make sure 'Add CAPTCHA administration links to forms' is appended to the
  // webform.
  // @see /admin/config/people/captcha
  // @see captcha_form_alter()
  if (!isset($form['captcha']) || !isset($form['captcha']['add_captcha'])) {
    return $form;
  }

  /** @var \Drupal\webform\WebformSubmissionForm $form_object */
  $form_object = $form_state
    ->getFormObject();
  $webform = $form_object
    ->getWebform();

  // Determine if the current user can update this webform via the UI.
  $has_update_access = $webform
    ->access('update') && \Drupal::moduleHandler()
    ->moduleExists('webform_ui');

  // If the webform has a CAPTCHA element, display a link to edit the element.
  $elements = $webform
    ->getElementsInitializedAndFlattened();
  foreach ($elements as $key => $element) {
    if (WebformElementHelper::isType($element, 'captcha')) {

      // Update the details element's title.
      $form['captcha']['#title'] = t('CAPTCHA: challenge enabled');

      // Replace 'Place a CAPTCHA here for untrusted users' link with link to
      // edit CAPTCHA element for this webform.
      if ($has_update_access) {
        $route_name = 'entity.webform_ui.element.edit_form';
        $route_parameters = [
          'webform' => $webform
            ->id(),
          'key' => $key,
        ];
        $route_options = [
          'query' => Drupal::destination()
            ->getAsArray(),
        ];
        $form['captcha']['add_captcha'] = [
          '#type' => 'link',
          '#title' => t('Untrusted users will see a CAPTCHA element on this webform.'),
          '#url' => Url::fromRoute($route_name, $route_parameters, $route_options),
          '#prefix' => '<em>',
          '#suffix' => '</em>',
        ];
      }
      else {
        $form['captcha']['add_captcha'] = [
          '#markup' => t('Untrusted users will see a CAPTCHA element on this webform.'),
          '#prefix' => '<em>',
          '#suffix' => '</em>',
        ];
      }
      return $form;
    }
  }

  // Replace 'Place a CAPTCHA here for untrusted users' link with link to
  // add CAPTCHA element to this webform.
  if ($has_update_access) {
    $route_name = 'entity.webform_ui.element.add_form';
    $route_parameters = [
      'webform' => $webform
        ->id(),
      'type' => 'captcha',
    ];
    $route_options = [
      'query' => Drupal::destination()
        ->getAsArray(),
    ];
    $form['captcha']['add_captcha'] = [
      '#type' => 'link',
      '#title' => t('Add CAPTCHA element to this webform for untrusted users.'),
      '#url' => Url::fromRoute($route_name, $route_parameters, $route_options),
    ];
  }
  else {
    $form['captcha']['add_captcha'] = [
      '#type' => 'webform_message',
      '#message_message' => t('CAPTCHA should be added as an element to this webform.'),
      '#message_type' => 'warning',
    ];
  }
  return $form;
}