ProcessorAddForm.php in Purge 8.3
File
modules/purge_ui/src/Form/ProcessorAddForm.php
View source
<?php
namespace Drupal\purge_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\purge\Plugin\Purge\Processor\ProcessorsServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProcessorAddForm extends ConfigFormBase {
use CloseDialogTrait;
protected $purgeProcessors;
public final function __construct(ProcessorsServiceInterface $purge_processors) {
$this->purgeProcessors = $purge_processors;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('purge.processors'));
}
protected function getEditableConfigNames() {
return [];
}
public function getFormId() {
return 'purge_ui.processor_add_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$definitions = $this->purgeProcessors
->getPlugins();
$form = parent::buildForm($form, $form_state);
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$options = [];
foreach ($this->purgeProcessors
->getPluginsAvailable() as $plugin_id) {
$options[$plugin_id] = $this
->t("@label: @description", [
'@label' => $definitions[$plugin_id]['label'],
'@description' => $definitions[$plugin_id]['description'],
]);
}
$form['id'] = [
'#default_value' => count($options) ? key($options) : NULL,
'#type' => 'radios',
'#options' => $options,
];
$form['actions']['submit'] = [
'#access' => count($options),
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t("Add"),
'#ajax' => [
'callback' => '::addProcessor',
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#weight' => -10,
'#ajax' => [
'callback' => '::closeDialog',
],
];
return $form;
}
public function addProcessor(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$id = $form_state
->getValue('id');
$response
->addCommand(new CloseModalDialogCommand());
if (in_array($id, $this->purgeProcessors
->getPluginsAvailable())) {
$enabled = $this->purgeProcessors
->getPluginsEnabled();
$enabled[] = $id;
$this->purgeProcessors
->setPluginsEnabled($enabled);
$response
->addCommand(new ReloadConfigFormCommand('edit-queue'));
}
return $response;
}
}