public function CaptchaQuestionsSettingsForm::buildForm in Captcha Questions 8
Form constructor.
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 The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ CaptchaQuestionsSettingsForm.php, line 90
Class
- CaptchaQuestionsSettingsForm
- Displays the captcha_questions settings form.
Namespace
Drupal\captcha_questions\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'core/drupal.ajax';
$form['description'] = [
'#type' => 'markup',
'#markup' => $this
->t("This is just a very simple mechanism to protect from automated bots. To make it as easy for real humans, consider providing the answer with the question. Example '<em>What is Mickeys last name? It's \"Mouse\"</em>'. You could also do simply 'What is 1+1?', but some bots may figure that out."),
];
$form['configuration'] = [
'#type' => 'details',
'#title' => $this
->t('Logging options'),
// Controls the HTML5 'open' attribute. Defaults to FALSE.
'#open' => TRUE,
];
// Disables checkbox if dblog module is not enabled.
$form['configuration']['captcha_questions_watchdog'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Log to watchdog'),
'#description' => $this
->t('Check this box to log all failed submissions to watchdog'),
'#default_value' => $this
->config('captcha_questions.settings')
->get('captcha_questions_watchdog'),
'#disabled' => $this->moduleHandler
->moduleExists('dblog') ? FALSE : TRUE,
];
// Disables checkbox if captcha_questions_dblog module is not enabled.
$form['configuration']['captcha_questions_dblog'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable logging internal database table'),
'#description' => $this
->t('Check this box to log all failed submissions in separate database table'),
'#default_value' => $this
->config('captcha_questions.settings')
->get('captcha_questions_dblog'),
'#disabled' => $this->moduleHandler
->moduleExists('captcha_questions_dblog') ? FALSE : TRUE,
];
$form['settings'] = [
'#type' => 'details',
'#title' => $this
->t('CAPTCHA'),
// Controls the HTML5 'open' attribute. Defaults to FALSE.
'#open' => TRUE,
];
$question = $this
->config('captcha_questions.settings')
->get('captcha_questions_question');
$form['settings']['captcha_questions_question'] = [
'#type' => 'textfield',
'#title' => $this
->t('Question to ask'),
'#default_value' => $question,
'#maxlength' => 256,
'#required' => TRUE,
];
$answers = $this
->config('captcha_questions.settings')
->get('captcha_questions_answers');
$form['settings']['captcha_questions_answers'] = [
'#type' => 'textarea',
'#title' => 'Accepted answer(s)',
'#default_value' => implode("\n", $answers),
'#description' => $this
->t('Please provide one or more accepted answers, one per line. Answers are case-insensitive'),
'#format' => NULL,
'#required' => TRUE,
];
$description = $this
->config('captcha_questions.settings')
->get('captcha_questions_description');
$form['settings']['captcha_questions_description'] = [
'#type' => 'textfield',
'#title' => $this
->t('Description'),
'#default_value' => $description,
'#maxlength' => 256,
];
$form['forms'] = [
'#type' => 'details',
'#title' => $this
->t('Form protection'),
// Controls the HTML5 'open' attribute. Defaults to FALSE.
'#open' => TRUE,
'#prefix' => '<div id="captcha-questions-form-id-wrapper">',
'#suffix' => '</div>',
];
// Getting form_ids.
if (!$form_state
->getValue('form_ids')) {
$form_state
->setValue('form_ids', $this
->captchaQuestionsGetFormIds());
}
// Adding new custom form_ids.
if ($form_state
->getValue('add_form_name') != '') {
$form_ids = $form_state
->getValue('form_ids');
$selected_all_form_ids = $form_state
->getValue('captcha_questions_form_ids');
$selected_all_form_ids = array_flip($selected_all_form_ids);
$all_form_ids = array_merge($form_ids, $selected_all_form_ids);
$all_form_ids[] = $form_state
->getValue('add_form_name');
$form_state
->setValue('form_ids', $all_form_ids);
}
// Getting previously selected form_ids.
$form_state
->setValue('selected_form_ids', $this
->config('captcha_questions.settings')
->get('captcha_questions_form_ids'));
// Create select list with previously selected form_ids as selected.
$form['forms']['captcha_questions_form_ids'] = [
'#type' => 'checkboxes',
'#options' => array_combine($form_state
->getValue('form_ids'), $form_state
->getValue('form_ids')),
'#default_value' => array_intersect($form_state
->getValue('selected_form_ids'), $form_state
->getValue('form_ids')),
'#title' => $this
->t('What forms should be protected?'),
];
// Adding node title and link to webform_ids options text.
if ($this->moduleHandler
->moduleExists('webform')) {
$webform_forms = $this
->captchaQuestionsGetWebforms();
foreach ($webform_forms as $webform_id => $webform) {
$url = Url::fromUri('base://form/' . $webform['id']);
$webform_node_link = Link::fromTextAndUrl($webform['title'], $url)
->toString();
$webform_form_id_option_text = $webform_id . ' (' . $webform_node_link . ')';
$form['forms']['captcha_questions_form_ids']['#options'][$webform_id] = $webform_form_id_option_text;
}
}
$form['forms']['add_form_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom form_id'),
'#default_value' => '',
'#maxlength' => 256,
];
$form['forms']['add_form'] = [
'#type' => 'button',
'#value' => $this
->t('Add custom form_id'),
'#submit' => [
'captchaQuestionsAddForm',
],
'#ajax' => [
'callback' => '::captchaQuestionsAddFormCallback',
'wrapper' => 'captcha-questions-form-id-wrapper',
],
];
return parent::buildForm($form, $form_state);
}