QueuerDeleteForm.php in Purge 8.3
File
modules/purge_ui/src/Form/QueuerDeleteForm.php
View source
<?php
namespace Drupal\purge_ui\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\purge\Plugin\Purge\Queuer\QueuersServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class QueuerDeleteForm extends ConfirmFormBase {
use CloseDialogTrait;
protected $purgeQueuers;
protected $queuer;
public final function __construct(QueuersServiceInterface $purge_queuers) {
$this->purgeQueuers = $purge_queuers;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('purge.queuers'));
}
protected function getEditableConfigNames() {
return [];
}
public function getFormId() {
return 'purge_ui.queuer_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->queuer = $this->purgeQueuers
->get($form_state
->getBuildInfo()['args'][0]);
$form = parent::buildForm($form, $form_state);
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->getConfirmText(),
'#ajax' => [
'callback' => '::deleteQueuer',
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('No'),
'#weight' => -10,
'#ajax' => [
'callback' => '::closeDialog',
],
];
return $form;
}
public function getConfirmText() {
return $this
->t('Yes, delete this queuer!');
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete the @label queuer?', [
'@label' => $this->queuer
->getLabel(),
]);
}
public function getCancelUrl() {
return NULL;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function deleteQueuer(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$enabled = $this->purgeQueuers
->getPluginsEnabled();
$id = $form_state
->getBuildInfo()['args'][0];
if (in_array($id, $enabled)) {
foreach ($enabled as $i => $plugin_id) {
if ($id === $plugin_id) {
unset($enabled[$i]);
}
}
$this->purgeQueuers
->setPluginsEnabled($enabled);
$response
->addCommand(new ReloadConfigFormCommand('edit-queue'));
}
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
}