3 namespace Drupal\workflows\Form;
5 use Drupal\workflows\WorkflowInterface;
6 use Drupal\Core\Form\ConfirmFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11 * Builds the form to delete states from Workflow entities.
13 class WorkflowStateDeleteForm extends ConfirmFormBase {
16 * The workflow entity the state being deleted belongs to.
18 * @var \Drupal\workflows\WorkflowInterface
23 * The state being deleted.
32 public function getFormId() {
33 return 'workflow_state_delete_form';
39 public function getQuestion() {
40 return $this->t('Are you sure you want to delete %state from %workflow?', ['%state' => $this->workflow->getTypePlugin()->getState($this->stateId)->label(), '%workflow' => $this->workflow->label()]);
46 public function getCancelUrl() {
47 return $this->workflow->toUrl();
53 public function getConfirmText() {
54 return $this->t('Delete');
61 * An associative array containing the structure of the form.
62 * @param \Drupal\Core\Form\FormStateInterface $form_state
63 * The current state of the form.
64 * @param \Drupal\workflows\WorkflowInterface $workflow
65 * The workflow entity being edited.
66 * @param string|null $workflow_state
67 * The workflow state being deleted.
72 public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_state = NULL) {
73 if (!$workflow->getTypePlugin()->hasState($workflow_state)) {
74 throw new NotFoundHttpException();
76 $this->workflow = $workflow;
77 $this->stateId = $workflow_state;
79 if ($this->workflow->getTypePlugin()->workflowStateHasData($this->workflow, $this->workflow->getTypePlugin()->getState($this->stateId))) {
80 $form['#title'] = $this->getQuestion();
81 $form['description'] = ['#markup' => $this->t('This workflow state is in use. You cannot remove this workflow state until you have removed all content using it.')];
85 return parent::buildForm($form, $form_state);
91 public function submitForm(array &$form, FormStateInterface $form_state) {
93 $workflow_label = $this->workflow->getTypePlugin()->getState($this->stateId)->label();
96 ->deleteState($this->stateId);
97 $this->workflow->save();
99 drupal_set_message($this->t(
100 'State %label deleted.',
101 ['%label' => $workflow_label]
104 $form_state->setRedirectUrl($this->getCancelUrl());