ContextDisableForm.php in Context 8.4
File
modules/context_ui/src/Form/ContextDisableForm.php
View source
<?php
namespace Drupal\context_ui\Form;
use Drupal\Core\Url;
use Drupal\context\ContextManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContextDisableForm extends EntityConfirmFormBase {
protected $contextManager;
public function __construct(ContextManager $contextManager) {
$this->contextManager = $contextManager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('context.manager'));
}
public function getQuestion() {
return $this
->t('Are you sure you want to %status the %label context?', [
'%status' => $this->entity
->disabled() ? "enable" : "disable",
'%label' => $this->entity
->getLabel(),
]);
}
public function getDescription() {
return $this
->t('This action will %status the %label context.', [
'%status' => $this->entity
->disabled() ? "enable" : "disable",
'%label' => $this->entity
->getLabel(),
]);
}
public function getCancelUrl() {
return new Url('entity.context.collection');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
if ($this
->getRequest()
->isXmlHttpRequest()) {
unset($form['actions']['cancel']);
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $formState) {
$this->entity
->disable();
$this
->messenger()
->addMessage($this
->t('The context %title has been %status.', [
'%title' => $this->entity
->getLabel(),
'%status' => $this->entity
->disabled() ? "disabled" : "enabled",
]));
$formState
->setRedirectUrl($this
->getCancelUrl());
}
}