Problem/Motivation
Core builds Drupal\commerce\Context by hand in 9 places, and the copies have drifted: AvailabilityOrderProcessor and UnitPriceWidget leave out the time (so they use "now"), while OrderRefresh, OrderItemEditForm and BuyXGetY pass the order's calculation date.
Contrib has no single place to add data to the Context, and any new Context input (such as the organization, see #3638816: [META] Commerce 4.x roadmap) would have to be added to every call site by hand.
Proposed resolution
Add two services that build every Context in core, and an event both dispatch.
// commerce_store interface CurrentContextInterface { // Context for the current request: current user, the given store or the // current store, current time. The base Context is memoized per request and store. public function getContext(?StoreInterface $store = NULL, array $data = []): Context; public function getCacheableMetadata(): CacheableMetadata; } // commerce_order interface OrderContextFactoryInterface { // Context for an order: its customer, its store, getCalculationDate(). // Deterministic, not memoized. public function createForOrder(OrderInterface $order, array $data = []): Context; }
- Event: both services dispatch
ContextEvents::CREATEbefore building the Context. It exposes the source (request or the order), customer, store, time and a mutable$dataarray.Contextstays final with getters only. - Caller data (e.g.
field_namefrom the formatters) is merged after the event, so the event runs once per memoized base Context, not once per rendered price. - Cacheability:
getCacheableMetadata()returns theuserandstorecache contexts; the formatters add it to their render arrays. CurrentContextInterfacelives incommerce_storebecause it needscommerce_store.current_store, andcommercedoes not depend oncommerce_store.
Call sites
| Call site | Moves to |
|---|---|
PriceCalculatedFormatter (commerce_order) |
CurrentContextInterface |
PriceCalculatedFormatter (commerce_price) |
CurrentContextInterface |
AddToCartForm::prepareOrderItem() |
CurrentContextInterface, with the store from selectStore() |
PurchasedEntityAvailableConstraintValidator |
Both: without an order, CurrentContextInterface with the selected store; on a draft order, OrderContextFactoryInterface |
OrderRefresh |
OrderContextFactoryInterface |
AvailabilityOrderProcessor |
OrderContextFactoryInterface |
BuyXGetY |
OrderContextFactoryInterface |
OrderItemEditForm |
OrderContextFactoryInterface |
UnitPriceWidget |
OrderContextFactoryInterface |
Tax rate resolvers and order processors that need the Context inject OrderContextFactoryInterface and call createForOrder($order_item->getOrder()). OrderRefresh sets order_id->entity before the processors run, so this returns the order being refreshed. TaxRateResolverInterface::resolve() keeps its signature.
Remaining tasks
- Kernel tests for both services: subscriber data, caller data merged after the event, memoization per store.
- Test that
createForOrder()usesgetCalculationDate(). - Existing price and availability tests stay green.
User interface changes
None.
API changes
New services, interfaces and event. The constructors of the classes that move get a new argument, with no BC layer (4.x). new Context() keeps working; nothing is deprecated.
Behavior change: AvailabilityOrderProcessor and UnitPriceWidget now use the order's calculation date. Nothing changes for draft orders (still the request time); for placed orders it is the placed time.
Data model changes
None.
Comments
Comment #2
jsacksick commented