IRC conversation explaining this situation and potential solution:
15:12 bojanz: joshmiller: Look at \Drupal\commerce_product\LineItemTypeMap
15:12 bojanz: joshmiller: it caches this relationship: Product type -> Product variation type -> Line item type
15:13 bojanz: joshmiller: so if you ask for the line item type for a product type, it gives you one, without needing to load the first two config entities
15:13 joshmiller: ah, cool
15:13 bojanz: joshmiller: look at commerce_product_entity_bundle_create(), it clears that cache when a new product type is created
15:13 bojanz: (you can't edit the product type -> product variation type relationship afterwards)
15:14 bojanz: joshmiller: however, you can change the line item type specified on a product variation type
15:14 bojanz: joshmiller: so we need to write a hook that reacts on commerce_product_variation_type save
15:14 bojanz: joshmiller: if the save was an update, and the line item type changed, clear the cache
15:16 bojanz: joshmiller: there are no D8 concepts in this task :)
15:17 joshmiller: yay! hooks!
15:20 bojanz: joshmiller: now, I am adding a complication
15:20 bojanz: joshmiller: instead of using a normal hook, we'll use the method version of it
15:20 bojanz: joshmiller: if you create a public function postSave(EntityStorageInterface $storage, $update = TRUE) { on the ProductVariationType, it will function the same as a post save hook
15:21 bojanz: joshmiller: $update tells you whether the entity was modified or created, and $this->original->lineItemType gives you the original value to compare against
15:21 bojanz: joshmiller: Commerce already has examples of such postSave() methods, so you can look at them
15:22 bojanz: (the actual line for clearing the cache remains the same as in commerce_product_entity_bundle_create)
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | clearprodlineitemcache-2689535-05.patch | 978 bytes | joshmiller |
| #3 | clearprodlineitemcache-2689535-03.patch | 1.07 KB | joshmiller |
| #2 | clearprodlineitemcache-2689535-02.patch | 755 bytes | joshmiller |
Comments
Comment #2
joshmillerI think this is what Bojan was getting at? Might be missing a hook, but I think this is right...
Comment #3
joshmillerBojan's feedback and an updated patch:
15:44 bojanz: joshmiller: you need a use statement for EntityStorageInterface, otherwise the code will crash
15:44 bojanz: joshmiller: you also need to check that the line item type setting has changed
15:44 bojanz: so after comparing $update you need to compare $this->lineItemType with $this->original->lineItemType
15:45 joshmiller: your mom needs to be compared
15:45 joshmiller: ok, will check back in
15:45 bojanz: she's uncomparable
My thoughts
I think the patch included is wrong and it should be either comparing the productType (instead of the lineItemType) or it should be a public postSave() in LineItemTypeForm. But, leaving this here because bojan who has been feeding me breadcrumbs to get me to the right place has disappeared into a fog of St. Patrick celebration.
Comment #4
joshmillerComment #5
bojanz commentedYou're adding the method to the wrong class. Such methods go to the entity class, not the form. So, ProductVariationType in this case.
Comment #6
joshmillerWell that makes more sense :D
Attached is an updated patch ... phpstorm doesn't like the $this->original but that may not be a problem... ?
Thanks for your patience bojan.
Comment #7
joshmillerComment #9
bojanz commentedBoom!