WorkflowTransitionEventSubscriber.php in State Machine 8
File
tests/modules/state_machine_test/src/EventSubscriber/WorkflowTransitionEventSubscriber.php
View source
<?php
namespace Drupal\state_machine_test\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class WorkflowTransitionEventSubscriber implements EventSubscriberInterface {
protected $messenger;
public function __construct(MessengerInterface $messenger) {
$this->messenger = $messenger;
}
public static function getSubscribedEvents() {
$events = [
'entity_test_with_bundle.create.pre_transition' => 'onPreTransition',
'entity_test_with_bundle.create.post_transition' => 'onPostTransition',
'entity_test_with_bundle.pre_transition' => 'onGroupPreTransition',
'entity_test_with_bundle.post_transition' => 'onGroupPostTransition',
'state_machine.pre_transition' => 'onGenericPreTransition',
'state_machine.post_transition' => 'onGenericPostTransition',
];
return $events;
}
public function onPreTransition(WorkflowTransitionEvent $event) {
$this
->setMessage($event, 'pre-transition');
}
public function onPostTransition(WorkflowTransitionEvent $event) {
$this
->setMessage($event, 'post-transition');
}
public function onGroupPreTransition(WorkflowTransitionEvent $event) {
$this
->setMessage($event, 'group pre-transition');
}
public function onGroupPostTransition(WorkflowTransitionEvent $event) {
$this
->setMessage($event, 'group post-transition');
}
public function onGenericPreTransition(WorkflowTransitionEvent $event) {
$this
->setMessage($event, 'generic pre-transition');
}
public function onGenericPostTransition(WorkflowTransitionEvent $event) {
$this
->setMessage($event, 'generic post-transition');
}
protected function setMessage(WorkflowTransitionEvent $event, $phase) {
$this->messenger
->addMessage(new TranslatableMarkup('@entity_label (@field_name) - @state_label at @phase (workflow: @workflow, transition: @transition).', [
'@entity_label' => $event
->getEntity()
->label(),
'@field_name' => $event
->getFieldName(),
'@state_label' => $event
->getTransition()
->getToState()
->getLabel(),
'@workflow' => $event
->getWorkflow()
->getId(),
'@transition' => $event
->getTransition()
->getId(),
'@phase' => $phase,
]));
}
}