StateChange.php in Workbench Moderation Actions 8
File
src/Controller/StateChange.php
View source
<?php
namespace Drupal\workbench_moderation_actions\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\workbench_moderation\Entity\ModerationState;
use Drupal\workbench_moderation\ModerationInformationInterface;
use Drupal\workbench_moderation\StateTransitionValidation;
use Drupal\workbench_moderation_actions\AjaxReloadCommand;
use Symfony\Component\DependencyInjection\ContainerInterface;
class StateChange extends ControllerBase {
protected $moderationInfo;
protected $validation;
public function __construct(ModerationInformationInterface $moderationInformation, StateTransitionValidation $validation) {
$this->moderationInfo = $moderationInformation;
$this->validation = $validation;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('workbench_moderation.moderation_information'), $container
->get('workbench_moderation.state_transition_validation'));
}
public function change($entity_type_id, $entity_id, $from, $to) {
$latest_revision = $this->moderationInfo
->getLatestRevision($entity_type_id, $entity_id);
$latest_revision
->get('moderation_state')->target_id = $to;
$latest_revision
->save();
$this
->messenger()
->addMessage($this
->t('%entity_label got changed from %from to %to', [
'%entity_label' => $latest_revision
->label(),
'%from' => ModerationState::load($from)
->label(),
'%to' => ModerationState::load($to)
->label(),
]));
return (new AjaxResponse())
->addCommand(new AjaxReloadCommand());
}
public function access($entity_type_id, $entity_id, $from, $to) {
return AccessResult::allowedIf($this->validation
->userMayTransition($from, $to, $this
->currentUser()))
->cachePerPermissions();
}
}
Classes
Name |
Description |
StateChange |
Performs the state change, if user has access to do it. |