Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

docs: explain state machine events in the state machine guide - #2685

Open
Daniel Kirkland (d-kirkland) wants to merge 1 commit into
mainfrom
docs/state-machine-events
Open

Daniel Kirkland (d-kirkland) wants to merge 1 commit into
mainfrom
docs/state-machine-events

Conversation

@d-kirkland

Copy link
Copy Markdown
Contributor

Summary

  • Adds a 'Reacting to state changes' section to the state machine guide for plugin developers who subscribe to order, delivery, or transaction state changes.
  • Explains that events fire only when the state really changes, and gives the dispatch order: transition event, then the leave event, then the enter event.
  • Gives the real event names state_machine.<machine>_changed for the three order machines. It also shows how to use getTransitionSide() so that logic doesn't run twice.
  • Explains that state_enter.* and state_leave.* names carry an OrderStateMachineStateChangeEvent, sent again by the core order listener for the three order machines only.

Related links

Checklist

  • I reviewed affected links, code samples, and cross-references, including PageRef references where relevant. (Names and values were checked against the source at the tag linked above.)
  • I added or updated redirects in .gitbook.yaml if pages were moved, renamed, or deleted. (N/A, no page moved.)
  • I updated .wordlist.txt (and sorted it) if spellcheck flags new legitimate terms. (Check needed if CI Spellcheck flags a word.)
  • Any required dependent changes in downstream modules have already been merged and published. (N/A)
  • This pull request is ready for review.

Notes

  • This PR was fully AI generated (Claude Code) and requires human review. It has not been merged, approved or reviewed by the AI session. Please verify the claims and the changed text against the source before merging.
  • Type: documentation gap, an addition to an existing page. The gap was found by testing the Shopware developer-docs chatbot with AI-generated questions. It was not reported by developers.
  • Codebase files this text was checked against (tag v6.7.15.1): src/Core/Checkout/Order/Aggregate/OrderDelivery/OrderDeliveryStates.php L10-L10; src/Core/Checkout/Order/Aggregate/OrderDelivery/OrderDeliveryStates.php L13-L13; src/Core/Checkout/Order/Aggregate/OrderTransaction/OrderTransactionStates.php L10-L10; src/Core/Checkout/Order/Event/OrderStateMachineStateChangeEvent.php L41-L41; src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.php L111-L111; src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.php L125-L128; src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.php L143-L143; src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.php L159-L162; src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.php L51-L53; src/Core/Checkout/Order/OrderStates.php L10-L10; src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.php L16-L17; src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.php L32-L35; src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.php L39-L41; src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.php L44-L46; src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.php L79-L81; src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.php L84-L86; src/Core/System/StateMachine/Event/StateMachineTransitionEvent.php L51-L51; src/Core/System/StateMachine/StateMachineRegistry.php L133-L135; src/Core/System/StateMachine/StateMachineRegistry.php L175-L177; src/Core/System/StateMachine/StateMachineRegistry.php L252-L254; src/Core/System/StateMachine/StateMachineRegistry.php L263-L265; src/Core/System/StateMachine/StateMachineRegistry.php L277-L279; src/Core/System/StateMachine/StateMachineRegistry.php L286-L289.
  • How it was written and checked: drafted by an AI model from the platform source with every statement mapped to a quoted source line, each quote re-checked mechanically, every sentence re-checked by a second AI model against the source. Please check the cited lines yourself before merging.
  • Deliberately not covered because the source does not settle it: Under which name Symfony dispatches StateMachineTransitionEvent when no name is given, or how to subscribe to it; Whether state_enter.* or state_leave.* events are dispatched for other machines such as order_transaction_capture.state; Whether registering a business event makes it show up as a Flow Builder trigger; The order of listeners beyond the leave-then-enter dispatch order.
  • No other open pull request changes these pages (checked against all open pull requests on 2026-11-02, including the merge test).
  • Scope: main (Shopware 6.7) only.

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@d-kirkland Daniel Kirkland (d-kirkland) added Undocumented Feature Feature which is not documented yet and is considered for documentation Improvement PR created to append/modify info in the existing article Guide Guides section of dev docs labels Nov 2, 2026
@shopware-dev-docs-connector

shopware-dev-docs-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Developer Docs healthcheck

Status: Completed with success.
Repository: shopware/docs
Commit: 8f7ec4e
Preview: https://developer-documentation-oizrw3e2l-shopware-frontends.vercel.app
Workflow run: #5919

@d-kirkland

Copy link
Copy Markdown
Contributor Author

Agent 00-Docs (an AI agent, posting from the PR author's account). This PR adds a "Reacting to state changes" section to the state machine guide: the order of the dispatched events, the state_machine.<name>_changed event names, and how they relate to the state_enter and state_leave names.

How it was checked: the text was drafted from the Shopware platform source at tag v6.7.15.1 with every statement mapped to a quoted source line, and the quotes were re-checked mechanically. A second AI model then checked every sentence against the source, and a third AI check that saw only the final text found the supporting source line for each statement itself. Where those checks found a statement broader than the source allows, the text was narrowed and checked again. The docs lint (rumdl) and the style rules pass, and no other open pull request changes these pages.

A person still needs to confirm the content. Please say if a statement is wrong or if this belongs on a different page.

It will return an array of possible actions.
If your order was still "open", this would result in two entries: One for `cancel` and one for `process`.

## Reacting to state changes

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reacting to order state changes is a real plugin question, but this section has it upside down. It leads with the raw state_machine.*_changed events (two calls per change, filter side and state yourself) and only gets to state_enter.* at the very end, as a caveat. Those are what a plugin should subscribe to for order, delivery and payment states: same names as the flow triggers and app webhooks, one call, and the order comes loaded.

'state_enter.order_delivery.state.shipped' => 'onDeliveryShipped',

public function onDeliveryShipped(OrderStateMachineStateChangeEvent $event): void
{
    $order = $event->getOrder();
}

I'd cut the section down to that, a link to the flow reference for the full list, and StateMachineTransitionEvent (see below) for everything else. Keep the sentence about no-op transitions; dispatch order, getter lists and internals can go.


For each state change, the registry dispatches three events in this order:

1. A `Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent`, dispatched without an explicit event name. It provides `getEntityName()`, `getEntityId()`, `getFromPlace()`, `getToPlace()`, `getContext()`, and `getInternalComment()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you list the getters but not how to subscribe, and the description says the source doesn't settle that. It does: Symfony falls back to the class name, and core subscribes to StateMachineTransitionEvent::class itself (OrderStockSubscriber, CustomerMetaFieldSubscriber). That's the useful part: one call per transition of any state machine, with entity id and from/to state.

`StateMachineStateChangeEvent::getStateEventName()` returns a name built from the transition side, the state machine, and the state name, for example `state_enter.order.state.completed`.
This isn't the name the `StateMachineStateChangeEvent` is dispatched under.

Instead, the core `OrderStateChangeEventListener` subscribes to the three `state_machine.*_changed` events listed above and dispatches a `Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent` under the name returned by `getStateEventName()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OrderStateChangeEventListener is @internal, please keep it out of the docs. The contract is all a plugin needs: state_enter.<machine>.<state> and state_leave.<machine>.<state> come with an OrderStateMachineStateChangeEvent.

Instead, the core `OrderStateChangeEventListener` subscribes to the three `state_machine.*_changed` events listed above and dispatches a `Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent` under the name returned by `getStateEventName()`.
A listener for a name such as `state_enter.order_delivery.state.shipped` therefore receives an `OrderStateMachineStateChangeEvent`, which provides the order through `getOrder()`, not a `StateMachineStateChangeEvent`.
In the 6.7 source, this listener only handles the `order.state`, `order_delivery.state`, and `order_transaction.state` machines.
The same listener also registers the `state_enter` and `state_leave` names of the states it reads from the state repository as business events of the `OrderStateMachineStateChangeEvent` class, even though it dispatches these events only for the three order state machines.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the capture and refund triggers never fire? Nothing in core dispatches them, but Flow Builder offers them and the flow reference says "Triggers when a payment capture is fully completed". That's a bug to fix in core, not a sentence for a plugin guide. Please drop it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a fix PR for this: shopware/shopware#22434
Please remove this restriction when it's merged.

@Isengo1989

Copy link
Copy Markdown
Contributor

Daniel Kirkland (@d-kirkland) - you want to give this a go for correction based on Max's feedback ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Guide Guides section of dev docs Improvement PR created to append/modify info in the existing article Undocumented Feature Feature which is not documented yet and is considered for documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants