You are here

public function ConfigurationForm::buildForm in Purge 8.3

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 PluginConfigFormBase::buildForm

File

modules/purge_queuer_coretags/src/Form/ConfigurationForm.php, line 30

Class

ConfigurationForm
Configuration form for the Core Tags queuer.

Namespace

Drupal\purge_queuer_coretags\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('purge_queuer_coretags.settings');

  // Blacklist form elements (and ajax 'add more' logic).
  $form['blacklist'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Tag blacklist'),
    '#description' => $this
      ->t('You can exclude tags that Drupal invalidated by listing them here, only change this <b>if you know what you are doing!</b> The strings are matched as prefixes, so for example <code>config:</code> will match tags as <code>config:core.extension</code> and <code>config:block_list</code>.'),
  ];

  // Retrieve the existing blacklist and initiatlize the counter.
  $blacklist = $config
    ->get('blacklist');
  if (is_null($form_state
    ->get('blacklist_items_count'))) {
    if (empty($blacklist)) {
      $form_state
        ->set('blacklist_items_count', 1);
    }
    else {
      $form_state
        ->set('blacklist_items_count', count($blacklist));
    }
  }

  // Define the fields based on whats stored in form state.
  $max = $form_state
    ->get('blacklist_items_count');
  $form['blacklist']['blacklist'] = [
    '#tree' => TRUE,
    '#prefix' => '<div id="blacklist-wrapper">',
    '#suffix' => '</div>',
  ];
  for ($delta = 0; $delta < $max; $delta++) {
    if (!isset($form['blacklist']['blacklist'][$delta])) {
      $element = [
        '#type' => 'textfield',
        '#default_value' => isset($blacklist[$delta]) ? $blacklist[$delta] : '',
      ];
      $form['blacklist']['blacklist'][$delta] = $element;
    }
  }

  // Define the add button.
  $form['blacklist']['add'] = [
    '#type' => 'submit',
    '#name' => 'add',
    '#value' => $this
      ->t('Add prefix'),
    '#submit' => [
      [
        $this,
        'addMoreSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'addMoreCallback',
      ],
      'wrapper' => 'blacklist-wrapper',
      'effect' => 'fade',
    ],
  ];
  return parent::buildForm($form, $form_state);
}