WorkflowTransitionRevertForm.php in Workflow 8
File
src/Form/WorkflowTransitionRevertForm.php
View source
<?php
namespace Drupal\workflow\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\workflow\Entity\WorkflowTransition;
use Drupal\workflow\Entity\WorkflowTransitionInterface;
class WorkflowTransitionRevertForm extends EntityConfirmFormBase {
public function getFormId() {
return 'workflow_transition_revert_confirm';
}
public function getQuestion() {
$transition = $this->entity;
$state = $transition
->getFromState();
if (!$state) {
\Drupal::logger('workflow_revert')
->error('Invalid state', []);
$message = $this
->t('Invalid transition. Your information has been recorded.');
$this
->messenger()
->addError($message);
return [];
}
$question = $this
->t('Are you sure you want to revert %title to the "@state" state?', [
'@state' => $state
->label(),
'%title' => $transition
->label(),
]);
return $question;
}
public function getCancelUrl() {
$transition = $this->entity;
return $this
->getUrl($transition);
}
public function getConfirmText() {
return $this
->t('Revert');
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
return $this->entity;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (\Drupal::moduleHandler()
->moduleExists('rules')) {
rules_invoke_event('workflow_state_reverted', $this->entity);
}
$transition = $this
->prepareRevertedTransition($this->entity);
$previous_sid = $transition
->getToSid();
$transition
->force(TRUE);
$new_sid = workflow_execute_transition($transition, TRUE);
$comment = $previous_sid == $new_sid ? $this
->t('State is reverted.') : $this
->t('State could not be reverted.');
$this
->messenger()
->addWarning($comment);
$form_state
->setRedirectUrl($this
->getUrl($transition));
}
protected function prepareRevertedTransition(WorkflowTransitionInterface $transition) {
$user = \Drupal::currentUser();
$entity = $transition
->getTargetEntity();
$field_name = $transition
->getFieldName();
$current_sid = workflow_node_current_state($entity, $field_name);
$previous_sid = $transition
->getFromSid();
$comment = $this
->t('State reverted.');
$transition = WorkflowTransition::create([
$current_sid,
'field_name' => $field_name,
]);
$transition
->setTargetEntity($entity);
$time = \Drupal::time()
->getRequestTime();
$transition
->setValues($previous_sid, $user
->id(), $time, $comment);
return $transition;
}
private function getUrl(WorkflowTransitionInterface $transition) {
$route_provider = \Drupal::service('router.route_provider');
if (count($route_provider
->getRoutesByNames([
'entity.node.workflow_history',
])) === 1) {
$url = new Url('entity.node.workflow_history', [
'node' => $transition
->getTargetEntityId(),
'field_name' => $transition
->getFieldName(),
]);
}
else {
$entity = $transition
->getTargetEntity();
$url = new Url($transition
->getTargetEntity()
->toUrl()
->getRouteName(), [
$entity
->getEntityTypeId() => $transition
->getTargetEntityId(),
'field_name' => $transition
->getFieldName(),
]);
}
return $url;
}
}