View source
<?php
namespace Drupal\state_machine\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StateTransitionConfirmForm extends ContentEntityConfirmFormBase {
protected $renderer;
protected $fieldName;
protected $transition;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->renderer = $container
->get('renderer');
return $instance;
}
public function getBaseFormId() {
return 'state_machine_transition_confirm_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $field_name = '', $transition_id = '') {
$state_item = $this->entity
->get($field_name)
->first();
$transition = $state_item
->getWorkflow()
->getTransition($transition_id);
$this->fieldName = $field_name;
$this->transition = $transition;
return parent::buildForm($form, $form_state);
}
public function getDescription() {
$state_item = $this->entity
->get($this->fieldName)
->first();
$items = [
$this
->t('<b>Transition</b>: @transition_label', [
'@transition_label' => $this->transition
->getLabel(),
]),
$this
->t('<b>@entity_type</b>: @entity_label', [
'@entity_type' => $this->entity
->getEntityType()
->getLabel(),
'@entity_label' => $this->entity
->label(),
]),
$this
->t('<b>From</b>: @from_state', [
'@from_state' => $state_item
->getOriginalLabel(),
]),
$this
->t('<b>To</b>: @to_state', [
'@to_state' => $this->transition
->getToState(),
]),
];
$description = [
'items' => [
'#type' => 'html_tag',
'#value' => implode('<br/>', $items),
'#tag' => 'p',
],
'warning' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => parent::getDescription(),
],
];
return $this->renderer
->renderPlain($description);
}
public function getQuestion() {
return $this
->t('Are you sure you want to apply this transition?');
}
public function getCancelUrl() {
return $this->entity
->toUrl('canonical');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$state_item = $this->entity
->get($this->fieldName)
->first();
if ($state_item
->isTransitionAllowed($this->transition
->getId())) {
$state_item
->applyTransition($this->transition);
$this->entity
->save();
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}