diff --git a/src/Entity/WorkflowTransition.php b/src/Entity/WorkflowTransition.php index 2f93fbe2477edd96c01c2d8001886d8bf76ebc86..71c33ee4a61678641fefa757e1e2ea9c4c23e22e 100644 --- a/src/Entity/WorkflowTransition.php +++ b/src/Entity/WorkflowTransition.php @@ -12,6 +12,8 @@ use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\user\Entity\User; use Drupal\user\UserInterface; +use Drupal\workflow\Event\WorkflowEvents; +use Drupal\workflow\Event\WorkflowTransitionEvent; use Drupal\workflow\WorkflowTypeAttributeTrait; /** @@ -77,6 +79,13 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition */ use WorkflowTypeAttributeTrait; + /** + * The event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + /* * Transition data: are provided via baseFieldDefinitions(). */ @@ -142,6 +151,7 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition public function __construct(array $values = [], $entityType = 'workflow_transition', $bundle = FALSE, array $translations = []) { // Please be aware that $entity_type and $entityType are different things! parent::__construct($values, $entityType, $bundle, $translations); + $this->eventDispatcher = \Drupal::service('event_dispatcher'); // This transition is not scheduled. $this->isScheduled = FALSE; // This transition is not executed, if it has no hid, yet, upon load. @@ -234,10 +244,12 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition $reference->set('entity', $entity); } + $this->dispatchEvent(WorkflowEvents::PRE_TRANSITION); // Avoid custom actions for subclass WorkflowScheduledTransition. if ($this->isScheduled() || $this->getEntityTypeId() != 'workflow_transition') { $result = parent::save(); + $this->dispatchEvent(WorkflowEvents::POST_TRANSITION); return $result; } @@ -274,9 +286,18 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition } } + $this->dispatchEvent(WorkflowEvents::POST_TRANSITION); return $result; } + /** + * {@inheritdoc} + */ + public function dispatchEvent($event_name) { + $transition_event = new WorkflowTransitionEvent($this); + $this->eventDispatcher->dispatch($transition_event, $event_name); + } + /** * {@inheritdoc} */ @@ -665,28 +686,27 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition * {@inheritdoc} */ public function setTargetEntity(EntityInterface $entity) { - if (!$entity) { - $this->entity_type = ''; - $this->entity_id = ''; - $this->revision_id = ''; - $this->delta = 0; // Only single value is supported. - $this->langcode = Language::LANGCODE_NOT_SPECIFIED; - return $this; - } + $this->entity_type = ''; + $this->entity_id = ''; + $this->revision_id = ''; + $this->delta = 0; // Only single value is supported. + $this->langcode = Language::LANGCODE_NOT_SPECIFIED; // If Transition is added via CommentForm, use the Commented Entity. - if ($entity->getEntityTypeId() == 'comment') { + if ($entity && $entity->getEntityTypeId() == 'comment') { /** @var \Drupal\comment\CommentInterface $entity */ $entity = $entity->getCommentedEntity(); } - $this->entity = $entity; - /** @var \Drupal\Core\Entity\RevisionableContentEntityBase $entity */ - $this->entity_type = $entity->getEntityTypeId(); - $this->entity_id = $entity->id(); - $this->revision_id = $entity->getRevisionId(); - $this->delta = 0; // Only single value is supported. - $this->langcode = $entity->language()->getId(); + if ($entity) { + $this->entity = $entity; + /** @ var \Drupal\Core\Entity\RevisionableContentEntityBase $entity */ + $this->entity_type = $entity->getEntityTypeId(); + $this->entity_id = $entity->id(); + $this->revision_id = $entity->getRevisionId(); + $this->delta = 0; // Only single value is supported. + $this->langcode = $entity->language()->getId(); + } return $this; } @@ -1131,7 +1151,7 @@ class WorkflowTransition extends ContentEntityBase implements WorkflowTransition $user = $transition->getOwner(); $user_name = ($user) ? $user->getAccountName() : 'unknown username'; $t_string = $this->getEntityTypeId() . ' ' . $this->id() . ' for workflow_type <i>' . $this->getWorkflowId() . '</i> ' . ($function ? ("in function '$function'") : ''); - $output[] = 'Entity = ' . $this->getTargetEntityTypeId() . '/' . (($entity) ? ($entity->bundle() . '/' . $entity->id()) : '___/0'); + $output[] = 'Entity type/id/vid = ' . $this->getTargetEntityTypeId() . '/' . (($entity) ? ($entity->bundle() . '/' . $entity->id() .'/'. $entity->getRevisionId()) : '___/0'); $output[] = 'Field = ' . $transition->getFieldName(); $output[] = 'From/To = ' . $transition->getFromSid() . ' > ' . $transition->getToSid() . ' @ ' . $time; $output[] = 'Comment = ' . $user_name . ' says: ' . $transition->getComment(); diff --git a/src/Event/WorkflowEvents.php b/src/Event/WorkflowEvents.php new file mode 100644 index 0000000000000000000000000000000000000000..36c534b515468b710a3d31874fdd454bcf924436 --- /dev/null +++ b/src/Event/WorkflowEvents.php @@ -0,0 +1,33 @@ +<?php + +namespace Drupal\workflow\Event; + +/** + * Defines events for the workflow module. + * + * @see \Drupal\workflow\Event\WorkflowPostTransitionEvent + */ +final class WorkflowEvents { + + /** + * Name of the event fired before a transition is executed. + * + * @Event + * + * @see \Drupal\workflow\Event\WorkflowTransitionEvent + * + * @var string + */ + const PRE_TRANSITION = 'workflow.pre_transition'; + + /** + * Name of the event fired after a transition is executed. + * + * @Event + * + * @see \Drupal\workflow\Event\WorkflowTransitionEvent + * + * @var string + */ + const POST_TRANSITION = 'workflow.post_transition'; +} diff --git a/src/Event/WorkflowTransitionEvent.php b/src/Event/WorkflowTransitionEvent.php new file mode 100644 index 0000000000000000000000000000000000000000..0806c0bff86fbbc871317c441b62514b8a905eb7 --- /dev/null +++ b/src/Event/WorkflowTransitionEvent.php @@ -0,0 +1,49 @@ +<?php + +namespace Drupal\workflow\Event; + +use Drupal\Component\EventDispatcher\Event; +use Drupal\workflow\Entity\WorkflowTransitionInterface; + +/** + * Defines the workflow transition event. + */ +class WorkflowTransitionEvent extends Event { + + /** + * The (scheduled or executed) transition. + * + * @var \Drupal\workflow\Entity\WorkflowTransitionInterface + */ + protected $transition; + + /** + * The entity. + * + * @var \Drupal\Core\Entity\ContentEntityInterface + */ + protected $entity; + + /** + * Constructs a new WorkflowTransitionEvent object. + * + * @param \Drupal\workflow\Entity\WorkflowTransitionInterface $transition + * The transition. + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity. + */ + public function __construct(WorkflowTransitionInterface $transition) { + $this->transition = $transition; + } + + /** + * Gets the transition. + * + * @return \Drupal\workflow\Entity\WorkflowTransitionInterface + * The transition. + */ + public function getTransition() { + return $this->transition; + } + +}