From 992ac8d2efed07e76f36339e4ff588a3b74a5c2e Mon Sep 17 00:00:00 2001 From: Andrei Mateescu <andrei@amateescu.me> Date: Tue, 29 Oct 2024 12:58:27 +0200 Subject: [PATCH] Ensure that workspaces are published by any transition to the 'published' state. --- entity_workflow.module | 6 +- ...EntityWorkflowWorkspaceEventSubscriber.php | 9 ++- src/Event/InitiateTransitionEvent.php | 63 +------------------ .../EntityWorkflowEventSubscriber.php | 4 +- 4 files changed, 14 insertions(+), 68 deletions(-) diff --git a/entity_workflow.module b/entity_workflow.module index 9887132..cf4632d 100644 --- a/entity_workflow.module +++ b/entity_workflow.module @@ -386,7 +386,11 @@ function entity_workflow_field_formatter_info_alter(array &$info) { function entity_workflow_transition(ContentEntityInterface $entity, $workflow_id, $transition_id, $log = '', $context = []) { $context['log'] = $log; - $event = new InitiateTransitionEvent($entity, $workflow_id, $transition_id, $context); + $workflow_field = entity_workflow_get_field($entity, $workflow_id); + $workflow = $workflow_field->getWorkflow(); + $transition = $workflow->getTypePlugin()->getTransition($transition_id); + + $event = new InitiateTransitionEvent($transition, $workflow, $entity, $workflow_field, $context); \Drupal::service('event_dispatcher')->dispatch($event, EntityWorkflowEvents::INITIATE_TRANSITION); } diff --git a/modules/entity_workflow_workspace/src/EventSubscriber/EntityWorkflowWorkspaceEventSubscriber.php b/modules/entity_workflow_workspace/src/EventSubscriber/EntityWorkflowWorkspaceEventSubscriber.php index 323f6a2..df707b1 100644 --- a/modules/entity_workflow_workspace/src/EventSubscriber/EntityWorkflowWorkspaceEventSubscriber.php +++ b/modules/entity_workflow_workspace/src/EventSubscriber/EntityWorkflowWorkspaceEventSubscriber.php @@ -32,17 +32,20 @@ class EntityWorkflowWorkspaceEventSubscriber implements EventSubscriberInterface * The initiate transition event. */ public function onInitiateTransaction(InitiateTransitionEvent $event) { - if ($event->getWorkflowId() === 'workspace') { + if ($event->getWorkflow()->id() === 'workspace') { /** @var \Drupal\workspaces\WorkspaceInterface $workspace */ $workspace = $event->getEntity(); - if ($event->getTransitionId() === 'publish') { + // Workspace publishing must be executed for any transition that goes to + // the 'published' state. + if ($event->getTransition()->to()->id() === 'published') { $workspace->publish(); // @todo When the workspace publisher will invoke a post publish event, // execute the workflow transition there and stop propagating this event. } - if ($event->getTransitionId() === 'unpublish') { + // Workspace reverting is tied to our specific 'unpublish' transition ID. + if ($event->getTransition()->id() === 'unpublish') { \Drupal::service('wse.workspace_reverter')->revert($workspace); } } diff --git a/src/Event/InitiateTransitionEvent.php b/src/Event/InitiateTransitionEvent.php index 4337af1..2ab5506 100644 --- a/src/Event/InitiateTransitionEvent.php +++ b/src/Event/InitiateTransitionEvent.php @@ -2,72 +2,11 @@ namespace Drupal\entity_workflow\Event; -use Drupal\Component\EventDispatcher\Event; -use Drupal\Core\Entity\ContentEntityInterface; - /** * Defines the initiate transition event. * * @see \Drupal\entity_workflow\Event\EntityWorkflowEvents */ -class InitiateTransitionEvent extends Event { - - /** - * @var \Drupal\Core\Entity\ContentEntityInterface - */ - protected $entity; - - /** - * @var string - */ - protected $workflowId; - - /** - * @var string - */ - protected $transitionId; - - /** - * @var array - */ - protected $transitionContext; - - /** - * Constructs a new InitiateTransitionEvent. - */ - public function __construct(ContentEntityInterface $entity, $workflow_id, $transition_id, $context = []) { - $this->entity = $entity; - $this->workflowId = $workflow_id; - $this->transitionId = $transition_id; - $this->transitionContext = $context; - } - - /** - * @return \Drupal\Core\Entity\ContentEntityInterface - */ - public function getEntity() { - return $this->entity; - } - - /** - * @return string - */ - public function getWorkflowId() { - return $this->workflowId; - } - - /** - * @return string - */ - public function getTransitionId() { - return $this->transitionId; - } - - /** - * @return array - */ - public function getTransitionContext() { - return $this->transitionContext; - } +class InitiateTransitionEvent extends EntityWorkflowTransitionEvent { } diff --git a/src/EventSubscriber/EntityWorkflowEventSubscriber.php b/src/EventSubscriber/EntityWorkflowEventSubscriber.php index 2a1fd85..1971c50 100644 --- a/src/EventSubscriber/EntityWorkflowEventSubscriber.php +++ b/src/EventSubscriber/EntityWorkflowEventSubscriber.php @@ -47,8 +47,8 @@ class EntityWorkflowEventSubscriber implements EventSubscriberInterface { public function onInitiateTransaction(InitiateTransitionEvent $event) { $entity = $event->getEntity(); - $workflow_field = entity_workflow_get_field($entity, $event->getWorkflowId()); - $workflow_field->applyTransitionById($event->getTransitionId(), $event->getTransitionContext()); + $workflow_field = entity_workflow_get_field($entity, $event->getWorkflow()->id()); + $workflow_field->applyTransition($event->getTransition(), $event->getContext()); $entity->_entityWorkflowEnforceNoNewRevision = TRUE; $entity->save(); -- GitLab