WorkflowStateDeleteForm.php in Drupal 10
File
core/modules/workflows/src/Form/WorkflowStateDeleteForm.php
View source
<?php
namespace Drupal\workflows\Form;
use Drupal\workflows\WorkflowInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class WorkflowStateDeleteForm extends ConfirmFormBase {
protected $workflow;
protected $stateId;
public function getFormId() {
return 'workflow_state_delete_form';
}
public function getQuestion() {
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(),
]);
}
public function getCancelUrl() {
return $this->workflow
->toUrl();
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function buildForm(array $form, FormStateInterface $form_state, WorkflowInterface $workflow = NULL, $workflow_state = NULL) {
if (!$workflow
->getTypePlugin()
->hasState($workflow_state)) {
throw new NotFoundHttpException();
}
$this->workflow = $workflow;
$this->stateId = $workflow_state;
if ($this->workflow
->getTypePlugin()
->workflowStateHasData($this->workflow, $this->workflow
->getTypePlugin()
->getState($this->stateId))) {
$form['#title'] = $this
->getQuestion();
$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.'),
];
return $form;
}
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$workflow_label = $this->workflow
->getTypePlugin()
->getState($this->stateId)
->label();
$this->workflow
->getTypePlugin()
->deleteState($this->stateId);
$this->workflow
->save();
$this
->messenger()
->addStatus($this
->t('State %label deleted.', [
'%label' => $workflow_label,
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}