- π©πͺGermany Rudi Teschner
The changes in the patch in #7 alone are not enough. For example are the event classes missing
- π©πͺGermany Rudi Teschner
Here is a version that works for me on a D10 website with D10.1.4 and workflow 8.x-1.7
- Status changed to Needs review
about 1 year ago 10:19am 8 October 2023 - last update
about 1 year ago Composer require failure - π¨πSwitzerland ayalon
I have tested #10 and using the patch. It works well with Drupal 10. Using this patch since ages.
- π¨πSwitzerland ayalon
Here is an updated patch following the event naming from Patch #7.
I prefer this approach as it is more flexible.
- π¨πSwitzerland ayalon
For some reasons the patch did not properly export. Please use this file:
-
johnv β
committed aaef3b4a on 8.x-1.x authored by
Rudi Teschner β
Issue #3038853 by sagesolutions, Rudi Teschner: Trigger events that can...
-
johnv β
committed aaef3b4a on 8.x-1.x authored by
Rudi Teschner β
- Status changed to Fixed
10 months ago 8:02pm 5 February 2024 - π³π±Netherlands johnv
Thanks, I committed a patch with some changes.
Only 1 TransactionEvent type was committed. The Event interface allows to only use the $event_name. So adding multiple subevents does not have added value.
All functions to read attributes of the $transition are not committed, while redundant. You can to $event->getTransition()->getFromSid().
For that, i assured the WorkflowTransitionInterface was completed here: π Improve WorkflowTransitionInterface Fixed
To make the impact of the commit smaller, the following was needed: π Restructure save() method Active
This way, the dispatchEvent() function can be inside, not around the save() function. This way, the events are also triggered for ScheduledTransitions.I hope these changes do not ruin you project code too much.
-
johnv β
committed 378c2651 on 8.x-1.x authored by
Rudi Teschner β
Issue #3038853 by sagesolutions, Rudi Teschner: Trigger events that can...
-
johnv β
committed 378c2651 on 8.x-1.x authored by
Rudi Teschner β
-
johnv β
committed 6280e9ce on 8.x-1.x authored by
Rudi Teschner β
Issue #3038853 by sagesolutions, Rudi Teschner: Trigger events that can...
-
johnv β
committed 6280e9ce on 8.x-1.x authored by
Rudi Teschner β
- π³π±Netherlands johnv
The EventSubscriber from #16 is added with dummy code.
- π¨π¦Canada sagesolutions
@johnv
Was it intentional to remove the entity and workflow from the WorkflowTransitionEvent?
In my eventsubscriber, I used the $event->getEntity() method to get the node.
+ + /** + * The workflow. + * + * @var \Drupal\workflow\Entity\WorkflowInterface + */ + protected $workflow; + + /** + * The entity. + * + * @var \Drupal\Core\Entity\ContentEntityInterface + */ + protected $entity; ... + public function __construct(WorkflowTransition $transition, WorkflowInterface $workflow, EntityInterface $entity) { + $this->transition = $transition; + $this->workflow = $workflow; + $this->entity = $entity; + } + + + /** + * Gets the workflow. + * + * @return \Drupal\workflow\Entity\WorkflowInterface + * The workflow. + */ + public function getWorkflow() { + return $this->workflow; + } + + /** + * Gets the entity. + * + * @return \Drupal\Core\Entity\EntityInterface + * The entity. + */ + public function getEntity() { + return $this->entity; + }
Also, I used a specific event to trigger my subscribed events. For example
'permit_status_workflow.permit_status_workflow_open.post_transition' => 'onOpenTransition',
Is this still possible? I think this functionality has been removed??
+ /** + * Dispatches a transition event for the given phase. + * + * @param string $phase + * The phase: pre_transition OR post_transition. + */ + protected function dispatchTransitionEvent($phase) { + + $workflow = $this->getWorkflow(); + + $event = new WorkflowTransitionEvent($this, $workflow, $this->getTargetEntity()); + $event_dispatcher = \Drupal::getContainer()->get('event_dispatcher'); + + $events = [ + // For example: 'my_custom_workflow.my_custom_transition.pre_transition'. + $this->getWorkflowId() . '.' . $this->getToSid() . '.' . $phase, + // For example: 'my_custom_workflow.pre_transition'. + $this->getWorkflowId() . '.' . $phase, + // For example: 'workflow.pre_transition'. + 'workflow.' . $phase, + ]; + + foreach ($events as $event_id) { + $event_dispatcher->dispatch($event, $event_id); + } + } +
- Status changed to Needs review
10 months ago 12:41am 6 February 2024 - π³π±Netherlands johnv
For the first block, you can make the following modifications. They will work anyway. As stated, you can get the info using the TransitionInterface:
perhaps changing the constructor to
public function __construct(WorkflowTransition $transition, WorkflowInterface $workflow = NULL, EntityInterface $entity = NULL) {}
or
public function __construct(WorkflowTransition $transition) {}
+ + /** + * The workflow. + * + * @var \Drupal\workflow\Entity\WorkflowInterface + */ + // protected $workflow; + + /** + * The entity. + * + * @var \Drupal\Core\Entity\ContentEntityInterface + */ + // protected $entity; ... + public function __construct(WorkflowTransition $transition, WorkflowInterface $workflow, EntityInterface $entity) { + $this->transition = $transition; + // $this->workflow = $workflow; + // $this->entity = $entity; + } + ... + public function getWorkflow() { + return $this->transition->getWorkflow(); // CHANGED + } + + public function getEntity() { + return $this->transition->getTargetEntity(); // CHANGED + }
- π³π±Netherlands johnv
@sagesolutions,
for your second block, yes, you still can do what you need to do. Please see below code suggestion:+ /** + * Dispatches a transition event for the given phase. + * + * @param string $phase + * The phase: pre_transition OR post_transition. + */ + protected function dispatchTransitionEvent($phase) { + + $events = [ + // For example: 'my_custom_workflow.my_custom_transition.pre_transition'. + $this->getWorkflowId() . '.' . $this->getToSid() . '.' . $phase, + // For example: 'my_custom_workflow.pre_transition'. + $this->getWorkflowId() . '.' . $phase, + // For example: 'workflow.pre_transition'. + 'workflow.' . $phase, + ]; + + $transition = $this->getTransition(); + foreach ($events as $event_id) { + $transition->dispatchEvent( $event_id); + } + } +
- π¨π¦Canada sagesolutions
Hi @johnv,
I like the getEntity() and getWorkflow() methods in the WorkflowTransistionEvent class. I can work with that. Please commit these updates :)+ public function getWorkflow() {
+ return $this->transition->getWorkflow(); // CHANGED
+ }
+
+ public function getEntity() {
+ return $this->transition->getTargetEntity(); // CHANGED
+ }For the subscribed events, I liked the ability of what was there before. Using the original patch, I can subscribe to each individual workflow change. For example:
public static function getSubscribedEvents(): array { return [ 'permit_status_workflow.permit_status_workflow_creation.post_transition' => 'onCreationTransition', 'permit_status_workflow.permit_status_workflow_draft.post_transition' => 'onDraftTransition', 'permit_status_workflow.permit_status_workflow_open.post_transition' => 'onOpenTransition', 'permit_status_workflow.permit_status_workflow_pending.post_transition' => 'onPendingTransition', 'permit_status_workflow.permit_status_workflow_approved.post_transition' => 'onApprovedTransition', 'permit_status_workflow.permit_status_workflow_declined.post_transition' => 'onDeclinedTransition', ]; }
But now, I can only subscribe to the one post transition event. Then in the called function, I need to check the transition to/from and break apart from there. This seems a bit messier than before.
- Status changed to RTBC
9 months ago 3:43pm 11 February 2024 - π¨π¦Canada sagesolutions
I've managed to update my EventSubscriber to work with the new workflow updates. Below is what works for me, maybe it will help others while upgrading.
/** * {@inheritdoc} */ public static function getSubscribedEvents(): array { return [ 'workflow.post_transition' => 'onWorkflowPostTransition', ]; } public function onWorkflowPostTransition(WorkflowTransitionEvent $event): void { switch ($event->getTransition()->getToSid()) { case "permit_status_workflow_pending" : $this->onPendingTransition($event); break; case "permit_status_workflow_open": $this->onOpenTransition($event); break; case "permit_status_workflow_approved": $this->onApprovedTransition($event); break; case "permit_status_workflow_declined": $this->onDeclinedTransition($event); break; } }
-
johnv β
committed 4f2e6552 on 8.x-1.x authored by
sagesolutions β
Issue #3038853 by sagesolutions: Trigger events that can be subscribed...
-
johnv β
committed 4f2e6552 on 8.x-1.x authored by
sagesolutions β
- Status changed to Fixed
9 months ago 3:55pm 14 February 2024 - π³π±Netherlands johnv
Some example code is added to the workflow_devel module.
Automatically closed - issue fixed for 2 weeks with no activity.