You are here

public function RiddlerSettingsForm::buildForm in Captcha Riddler 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/RiddlerSettingsForm.php, line 35

Class

RiddlerSettingsForm
Displays the Riddler settings form.

Namespace

Drupal\riddler\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // This can be rebuilt by ajax, so check for existing values. in $form_state.
  // Run through array values to reset keys in case one was ajax removed.
  $values = $form_state
    ->getValues();
  $riddles = isset($values['riddler']) ? $values['riddler'] : [];

  // Without checking if there was a triggering element, this would reload
  // the original active config instead of deleting the last riddle. The
  // triggering element would be the remove or add another buttons.
  if (empty($riddles) && empty($form_state
    ->getTriggeringElement())) {
    $riddles = $this
      ->config('riddler.settings')
      ->get('riddles');
  }

  // Add CSS for styling settings form.
  $form['#attached']['library'][] = 'riddler/base';

  // Initialize the counter if it hasn't been set. We do it this way so the
  // add more ajax callback can increment the max to create an empty riddle.
  $rebuild_info = $form_state
    ->getRebuildInfo();
  if (!isset($rebuild_info['riddler']['items_count'])) {

    // Nested to avoid unlikely conflicts with other modules.
    $form_state
      ->setRebuildInfo([
      'riddler' => [
        'items_count' => count($riddles) === 0 ? 1 : count($riddles),
      ],
    ]);
  }
  $max = $form_state
    ->getRebuildInfo()['riddler']['items_count'];
  $form['riddler'] = [
    '#tree' => TRUE,
    '#prefix' => '<div id="riddler">',
    '#suffix' => '</div>',
  ];
  $form['riddler']['help'] = [
    '#markup' => $this
      ->t('Add questions that you require users to answer. A random question will be presented to the user on forms as configured in the CAPTCHA settings. Allow more than one correct answer by entering a comma-separated list.'),
    '#prefix' => '<p>',
    '#suffix' => '</p>',
  ];

  // Build or rebuild the riddle fields.
  for ($delta = 0; $delta < $max; $delta++) {
    if (!isset($form['riddler'][$delta])) {
      $element = [
        'question' => [
          '#type' => 'textfield',
          '#title' => 'Riddle ' . ($delta + 1) . ' question',
          '#default_value' => isset($riddles[$delta]) ? $riddles[$delta]['question'] : '',
          '#prefix' => '<div id="row-riddle' . ($delta + 1) . '" class="row-riddle">',
        ],
        'response' => [
          '#type' => 'textfield',
          '#title' => 'Answer',
          '#default_value' => isset($riddles[$delta]) ? $riddles[$delta]['response'] : '',
        ],
        // Delete a row button.
        'delete' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Remove'),
          '#id' => 'riddle' . $delta,
          '#name' => 'riddle-remove-' . $delta,
          '#submit' => [
            [
              $this,
              'removeSubmit',
            ],
          ],
          '#limit_validation_errors' => [
            [],
          ],
          '#ajax' => [
            'callback' => [
              $this,
              'removeCallback',
            ],
            'wrapper' => 'riddler',
            'method' => 'replace',
            'effect' => 'fade',
          ],
          '#suffix' => '</div>',
        ],
      ];
      $form['riddler'][$delta] = $element;
    }
  }

  // Add a row button.
  $form['add'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add another riddle'),
    '#submit' => [
      [
        $this,
        'addMoreSubmit',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'riddler',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'addMoreCallback',
      ],
      'wrapper' => 'riddler',
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];
  return parent::buildForm($form, $form_state);
}