View source
<?php
namespace Drupal\purge_queuer_coretags\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\purge_ui\Form\QueuerConfigFormBase;
class ConfigurationForm extends QueuerConfigFormBase {
protected function getEditableConfigNames() {
return [
'purge_queuer_coretags.settings',
];
}
public function getFormId() {
return 'purge_queuer_coretags.configuration_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('purge_queuer_coretags.settings');
$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>.'),
];
$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));
}
}
$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;
}
}
$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);
}
public function addMoreSubmit(array &$form, FormStateInterface $form_state) {
$count = $form_state
->get('blacklist_items_count');
$count++;
$form_state
->set('blacklist_items_count', $count);
$form_state
->setRebuild();
}
public function addMoreCallback(array &$form, FormStateInterface $form_state) {
return $form['blacklist']['blacklist'];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$blacklist = [];
foreach ($form_state
->getValue('blacklist') as $prefix) {
if (!empty(trim($prefix))) {
$blacklist[] = $prefix;
}
}
$form_state
->setValue('blacklist', $blacklist);
}
public function submitFormSuccess(array &$form, FormStateInterface $form_state) {
$this
->config('purge_queuer_coretags.settings')
->set('blacklist', $form_state
->getValue('blacklist'))
->save();
}
}