You are here

function _webform_honeypot_form in Webform 6.x

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

Alter webform third party settings webforms to include Honeypot configuration.

Parameters

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

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

bool $honeypot: TRUE if honeypot protection is enabled.

int $honeypot_state: Flag that determines if honeypot protection is enabled, disabled, or can be set.

bool $time_restriction: TRUE if honeypot time restriction is enabled.

int $time_restriction_state: Flag that determines if honeypot time restriction is enabled, disabled, or can be set.

string|\Drupal\Core\StringTranslation\TranslatableMarkup $label: The label to displayed within the checkbox titles.

2 calls to _webform_honeypot_form()
honeypot_webform_admin_third_party_settings_form_alter in third_party_settings/webform.honeypot.inc
Implements hook_webform_admin_third_party_settings_form_alter().
honeypot_webform_third_party_settings_form_alter in third_party_settings/webform.honeypot.inc
Implements hook_webform_third_party_settings_form_alter().

File

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

Code

function _webform_honeypot_form(array &$form, FormStateInterface $form_state, $honeypot, $honeypot_state, $time_restriction, $time_restriction_state, $label) {
  $t_args = [
    '%label' => $label,
    ':href_honeypot' => Url::fromRoute('honeypot.config')
      ->toString(),
    ':href_webform' => Url::fromRoute('webform.config')
      ->toString(),
  ];

  // Honeypot.
  $form['third_party_settings']['honeypot'] = [
    '#type' => 'details',
    '#title' => t('Honeypot'),
    '#open' => TRUE,
    '#description' => t('Mitigate SPAM webform submissions using the <a href=":href_honeypot">honeypot</a> method.', $t_args),
  ];
  $form['third_party_settings']['honeypot']['honeypot'] = [
    '#type' => 'checkbox',
    '#title' => t('Protect %label with Honeypot', $t_args),
    '#default_value' => $honeypot,
    '#return_value' => TRUE,
  ];
  if ($honeypot_state !== WEBFORM_HONEYPOT_NEUTRAL) {
    $form['third_party_settings']['honeypot']['honeypot']['#attributes']['disabled'] = 'disabled';
    $form_state
      ->set('honeypot_disabled', TRUE);
    if ($honeypot_state === WEBFORM_HONEYPOT_ENABLED_ALL) {
      $form['third_party_settings']['honeypot']['honeypot']['#default_value'] = 1;
      $form['third_party_settings']['honeypot']['honeypot']['#description'] = t('<a href=":href_honeypot">Honeypot protection</a> is enabled for all webforms.', $t_args);
    }
    elseif ($honeypot_state === WEBFORM_HONEYPOT_ENABLED_WEBFORM) {
      $form['third_party_settings']['honeypot']['honeypot']['#default_value'] = 1;
      $form['third_party_settings']['honeypot']['honeypot']['#description'] = t('<a href=":href_webform">Honeypot protection</a> is enabled for all webforms.', $t_args);
    }
  }

  // Time limit.
  $form['third_party_settings']['honeypot']['time_restriction'] = [
    '#type' => 'checkbox',
    '#title' => t('Add time restriction to %label', $t_args),
    '#description' => '',
    '#default_value' => $time_restriction,
    '#return_value' => TRUE,
  ];
  if ($time_restriction_state !== WEBFORM_HONEYPOT_NEUTRAL) {
    $form['third_party_settings']['honeypot']['time_restriction']['#attributes']['disabled'] = 'disabled';
    $form_state
      ->set('time_restriction_disabled', TRUE);
    if ($time_restriction_state === WEBFORM_HONEYPOT_DISABLED_ALL) {
      $form['third_party_settings']['honeypot']['time_restriction']['#default_value'] = 0;
      $form['third_party_settings']['honeypot']['time_restriction']['#description'] = t('<a href=":href_honeypot">Time limit</a> is disabled for all webforms.', $t_args);
    }
    elseif ($time_restriction_state === WEBFORM_HONEYPOT_ENABLED_WEBFORM) {
      $form['third_party_settings']['honeypot']['time_restriction']['#default_value'] = 1;
      $form['third_party_settings']['honeypot']['time_restriction']['#description'] = t('<a href=":href_webform">Time limit</a> is enabled for all webforms.', $t_args);
    }
  }
  $form['third_party_settings']['honeypot']['time_restriction']['#description'] = ($form['third_party_settings']['honeypot']['time_restriction']['#description'] ? '<br/>' : '') . '<strong>' . t('Page caching will be disabled when a time restriction is applied to a webform.') . '</strong>';
  $form['#validate'][] = '_webform_honeypot_form_validate';
}