You are here

function honeypot_webform_submission_form_alter in Webform 6.x

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

Implements hook_webform_submission_form_alter().

File

third_party_settings/webform.honeypot.inc, line 204
Integrates third party settings on the Honeypot module's behalf.

Code

function honeypot_webform_submission_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Only add a Honeypot when a webform is initially load.
  // After a webform is submitted, via a multi-step webform and/or saving a draft,
  // we can skip adding a Honeypot.
  if ($form_state
    ->isSubmitted()) {
    return;
  }

  /** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
  $third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');

  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $form_state
    ->getFormObject()
    ->getEntity();
  $webform = $webform_submission
    ->getWebform();
  $options = [];
  $honeypot = (int) $third_party_settings_manager
    ->getThirdPartySetting('honeypot', 'honeypot') ?: $webform
    ->getThirdPartySetting('honeypot', 'honeypot');
  if ($honeypot) {
    $options[] = 'honeypot';
  }
  $time_restriction = (int) $third_party_settings_manager
    ->getThirdPartySetting('honeypot', 'time_restriction') ?: $webform
    ->getThirdPartySetting('honeypot', 'time_restriction');
  if ($time_restriction) {
    $options[] = 'time_restriction';
  }
  if ($options) {
    honeypot_add_form_protection($form, $form_state, $options);
  }
}