renderer = $container->get('renderer');
return $instance;
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return 'state_machine_transition_confirm_form';
}
/**
* Returns the transition object.
*
* @return \Drupal\state_machine\Plugin\Workflow\WorkflowTransition
* The transition object.
*/
public function getTransition() {
return $this->transition;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $field_name = '', $transition_id = '') {
/** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
$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);
}
/**
* {@inheritdoc}
*/
public function getDescription() {
/** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
$state_item = $this->entity->get($this->fieldName)->first();
$items = [
$this->t('Transition: @transition_label', ['@transition_label' => $this->transition->getLabel()]),
$this->t('@entity_type: @entity_label', ['@entity_type' => $this->entity->getEntityType()->getLabel(), '@entity_label' => $this->entity->label()]),
$this->t('From: @from_state', ['@from_state' => $state_item->getOriginalLabel()]),
$this->t('To: @to_state', ['@to_state' => $this->transition->getToState()]),
];
$description = [
'items' => [
'#type' => 'html_tag',
'#value' => implode('
', $items),
'#tag' => 'p',
],
'warning' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => parent::getDescription(),
],
];
return $this->renderer->renderPlain($description);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to apply this transition?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->entity->toUrl('canonical');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\state_machine\Plugin\Field\FieldType\StateItemInterface $state_item */
$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());
}
}