PurgerMoveForm.php in Purge 8.3
File
modules/purge_ui/src/Form/PurgerMoveForm.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\Purger\PurgersServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PurgerMoveForm extends ConfirmFormBase {
use CloseDialogTrait;
protected $id;
protected $definition;
protected $direction;
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_move_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$this->definition = $form_state
->getBuildInfo()['args'][0]['definition'];
$this->direction = $form_state
->getBuildInfo()['args'][0]['direction'];
$this->id = $form_state
->getBuildInfo()['args'][0]['id'];
$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' => '::movePurger',
],
];
$form['actions']['cancel'] = [
'#type' => 'submit',
'#value' => $this
->t('No'),
'#weight' => -10,
'#ajax' => [
'callback' => '::closeDialog',
],
];
return $form;
}
public function getConfirmText() {
return $this
->t('Yes!');
}
public function getQuestion() {
$label = [
'@label' => $this->purgePurgers
->getLabels()[$this->id],
];
if ($this->direction === 'up') {
return $this
->t('Do you want to move @label up in the execution order?', $label);
}
elseif ($this->direction === 'down') {
return $this
->t('Do you want to move @label down in the execution order?', $label);
}
return '';
}
public function getCancelUrl() {
return NULL;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
public function movePurger(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$response
->addCommand(new CloseModalDialogCommand());
if (isset($this->purgePurgers
->getPluginsEnabled()[$this->id])) {
$response
->addCommand(new ReloadConfigFormCommand('edit-purgers'));
if ($this->direction === 'up') {
$this->purgePurgers
->movePurgerUp($this->id);
}
elseif ($this->direction === 'down') {
$this->purgePurgers
->movePurgerDown($this->id);
}
}
return $response;
}
}
Classes
Name |
Description |
PurgerMoveForm |
Move purger {id} in the purger execution order - 'up' or 'down'. |