PurgerAddForm.php in Purge 8.3
File
modules/purge_ui/src/Form/PurgerAddForm.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\Purger\PurgersServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PurgerAddForm extends ConfigFormBase {
use CloseDialogTrait;
protected $purgePurgers;
public final function __construct(PurgersServiceInterface $purge_purgers) {
$this->purgePurgers = $purge_purgers;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('purge.purgers'));
}
protected function getEditableConfigNames() {
return [];
}
public function getFormId() {
return 'purge_ui.purger_add_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$available = $this->purgePurgers
->getPluginsAvailable();
$plugins = $this->purgePurgers
->getPlugins();
foreach ($plugins as $plugin_id => $definition) {
if (!in_array($plugin_id, $available)) {
unset($plugins[$plugin_id]);
}
else {
$plugins[$plugin_id] = $this
->t('<b>@label</b><br />@description', [
'@label' => $definition['label'],
'@description' => $definition['description'],
]);
}
}
$form['plugin_id'] = [
'#default_value' => count($plugins) ? key($plugins) : NULL,
'#type' => 'radios',
'#options' => $plugins,
];
$form['actions']['submit'] = [
'#access' => (bool) count($plugins),
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t("Add"),
'#ajax' => [
'callback' => '::addPurger',
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('Cancel'),
'#weight' => -10,
'#ajax' => [
'callback' => '::closeDialog',
],
];
return $form;
}
public function addPurger(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$enabled = $this->purgePurgers
->getPluginsEnabled();
$plugin_id = $form_state
->getValue('plugin_id');
$response
->addCommand(new CloseModalDialogCommand());
if (in_array($plugin_id, $this->purgePurgers
->getPluginsAvailable())) {
$enabled[$this->purgePurgers
->createId()] = $plugin_id;
$this->purgePurgers
->setPluginsEnabled($enabled);
$response
->addCommand(new ReloadConfigFormCommand('edit-purgers'));
}
return $response;
}
}