Problem/Motivation
Custom listing of products created through Views (using Product row display) and with Layout builder at the same time managing product display it is causing the listing to break with error:
Drupal\Component\Plugin\Exception\MissingValueContextException: Required contexts without a value: entity in Drupal\Core\Plugin\Context\ContextHandler->applyContextMapping() (line 150 in /var/www/ololo/web/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php)
Current \Drupal\commerce_product\ContextProvider\ProductVariationContext does not handle routes outside commerce product route
Steps to reproduce
Create view listing of Commerce Product with row Display product.
Enable layout builder and manage the display of Products with it.
Open page listing created with views.
Proposed resolution
Inject into current request Commerce product entity within hook_entity_prepare_view.
This hook is triggered enough early so that \Drupal\commerce_product\ContextProvider\ProductVariationContext can get that parameter.
Add a new condition in ProductVariationContext to load the default variant immediately.
Remaining tasks
None
User interface changes
None
API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #48 | 3180541-48.patch | 20.19 KB | tbkot |
| #23 | 3180541-23_Product_Variation_rendering_in_layout_builder.patch | 8.69 KB | dubs |
| #10 | product-variation-without-context-3180541-10.patch | 3.84 KB | danmer |
| #7 | 3180541-7.patch | 2.36 KB | junaidpv |
| #4 | interdiff-2-4.txt | 1.26 KB | valic |
Issue fork commerce-3180541
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3180541-product-variation-is
changes, plain diff MR !83
Comments
Comment #2
valicAttaching an initial quick patch to solve the issue.
Comment #3
valicComment #4
valicSmall changes to the patch in regards to request where we set attribute
Comment #5
lukasss commentedI'm not sure if this is related.
So
I have a product with LB (layout builder) enabled.
I am displaying variation fields in LB.
If the current product has no variations entity, then I get this error.
Comment #6
junaidpv@lukasss That is a different issue reported at #3185884: Layout Builder for product with empty variation field not workingComment #7
junaidpvPatch from #4 did not work for me. It looks wrong with implementation of
hook_entity_prepare_view().As described at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21... , the third parameter $displays is keyed by bundle name not by view mode name.
I corrected it and it is now working for me.
Comment #8
jsacksick commentedThis needs tests. I haven't worked on the layout builder integration myself and I don't really want to commit a fix blindly without making sure this doesn't break anything else.
Comment #9
junaidpvI don't think it needs to be committed. The above patch is just a workaround.
Comment #10
danmer commentedI have tested the patch on my side, it works as expected, but I have improved it a bit.
Comment #12
mglamanThis is due to the fact that the ProductVariationContext only works in a single instance.
You may want to investigate more in something like this code:
Comment #15
super_romeo commentedComment #16
super_romeo commentedComment #17
proweb.ua commentedcommerce 8.x-2.29
10# not applay
Comment #18
super_romeo commentedPatch #17 works as expected.
Thank you, @Christian.wiedemann!
Comment #19
rossidrup commentedI have exactly the same error...will this be fixed? how to apply #17? it is a merged request? Where is the patch url?
Comment #20
rossidrup commentedi applied the patch, the error is gone, but in a view i get so many duplicates of the same sku, before I did not have any when I used fields in view, so is this related to patch?
Comment #21
mile23MR includes .idea/ PHPStorm config, which is probably not desired.
Comment #22
dubs commentedThis doesn't work for views unfortunately. The LazyContextRepository statically caches the first product variation, so in views with multiple products, the product variation will always be the first variation in the list of rendered entities, i.e. all results in the view will only show data for the first product's variation. I'm working on a fix but it may not be possible without overriding the LazyContextRepositoy.
Comment #23
dubs commentedSo I've overridden the context repository in commerce_product. I'm not sure if this is the best approach, but take a look and let's see if we can get this merged and working.
The patch is based of the diff from Merge request !83, with an extra service override.
Comment #25
tbkot commentedComment #26
proweb.ua commented#23 works
drupal 9.5.5
Comment #27
jsacksick commentedSetting the status to "Needs work" as we really need tests coverage to ensure this doesn't break in the future again.
Comment #28
tbkot commentedComment #29
tbkot commentedComment #30
morbus iff#23 works for me. I've not been able to get the MR to apply.
Comment #33
jsacksick commentedCommited the patch from the MR, thanks everyone!! Committed.
Comment #36
jsacksick commentedA new release including this fix was tagged yesterday (2.34) and it already introduced 2 critical issues so decided to revert it and we'll target a proper fix in 2.36 I guess.
Comment #37
jsacksick commentedSee #3351579: Commit of 3180541 leads to out of memory error and #3351333: Error: Call to undefined method Symfony\Component\HttpFoundation\RequestStack::getMasterRequest() for reference.
Comment #38
tbkot commentedI’ve found one more problem with those changes. As it uses "hook_entity_prepare_view" when we try to render multiple products programmatically it adds the last one to the request attributes. As a result, we have incorrect data from variation(price, SKU, etc.) because it will be taken from the last product in the render list.
Comment #39
tbkot commentedOne more issue with the patch is that Drupal does change the already existing context. So, for example, if we going to display multiple products, even though we find a way to set the correct variation entity in the context for every product on the page, the data from the first variation will be shown for all of them.
Comment #40
morbus iffWhile #23 works for me without any of the problems other folks have experienced (yet), I think I have an example of #39 too. On product A, I have a "Similar products" entity reference field, which references product B. Next, I'll make a view called "Similar Products", intended to be displayed on product A. It'll take the current entity as the argument (A), relationship load the entity from A's Similar Product field (B), then relationship load the variants from that relationship (B's variants). At this point, if I display the product variant View field "Image", then B's variant's images will be displayed (both in the View preview and on the product A entity). But, if I tweak the View from Fields display to Rendered entity display, then I will see the correct B's variant's image on the View preview but, on the product A entity, the image will always show Product A's Image instead.
Comment #41
tbkot commentedComment #42
morbus iff@tBKoT: patch branch needs to be rebased.
Comment #43
mav_fly commentedwe have the same problem. Is there already a working patch for this problem?
We use :
commerce: 8.x-2.36
D10.1.6
PHP 8.2.13
Comment #44
jsacksick commented@Mav_fly: Have you tried the patch from the merge request?
Comment #45
mav_fly commentedThe code of referred modeules does not correspond with te proposed changes. We use the latest Drupal code 10.1.6 and php version 8.2.13.S
So the patches can't be executed.
Comment #46
jsacksick commented@tBKoT:
We're still using the hook_entity_prepare_view(), so I guess this comment is still valid? Because this hasn't been RTBCED yet, I'm a bit reluctant to get this in for 2.37, so we should look aim to include the fix in 2.38.
Comment #47
jsacksick commented@tBKoT: For the new event subscriber introduced, could we conditionally register it using a service provider instead?
Also, I wonder if we can get an RTBC here...
@Morbus Iff? @Dubs? Anyone?
Comment #48
tbkot commentedPatch with the latest changes in MR 83
Comment #51
jsacksick commented@tBKot: Thank you! Committed!
Comment #55
morbus iffWith this patch committed, looks like #40 is still an issue for me.
Comment #56
tbkot commentedHi @morbus-iff
Could you describe a bit more how I can reproduce the issue? I've tried a bunch of manipulations to catch the issue you faced with. Is it possible to add step by step on all your way to add "Similar products" on the product page? Especially the display configuration for product and variation as you use "Rendered entity" display
Comment #58
almador commentedWhen applying the patch on Drupal 10.3.1
Could not apply patch! Skipping. The error was: Cannot apply patch https://www.drupal.org/files/issues/2024-02-05/3180541-48.patchI'm using the Commerce Core 2.39 (tried also with dev version)
My error code:
Drupal\Component\Plugin\Exception\MissingValueContextException: Required contexts without a value: commerce_productHere is the Call stack: