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

Issue fork commerce-3180541

Command icon 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:

Comments

valic created an issue. See original summary.

valic’s picture

Status: Active » Needs review
StatusFileSize
new2.03 KB

Attaching an initial quick patch to solve the issue.

valic’s picture

Issue summary: View changes
valic’s picture

StatusFileSize
new2.33 KB
new1.26 KB

Small changes to the patch in regards to request where we set attribute

lukasss’s picture

I'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.

junaidpv’s picture

junaidpv’s picture

StatusFileSize
new2.36 KB

Patch 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.

jsacksick’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This 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.

junaidpv’s picture

I don't think it needs to be committed. The above patch is just a workaround.

danmer’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new3.84 KB

I have tested the patch on my side, it works as expected, but I have improved it a bit.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 10: product-variation-without-context-3180541-10.patch, failed testing. View results

mglaman’s picture

This 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:

      // @todo Simplify this logic once EntityTargetInterface is available
      // @see https://www.drupal.org/project/drupal/issues/3054490
      elseif (strpos($this->routeMatch->getRouteName(), 'layout_builder') !== FALSE) {

Christian.wiedemann made their first commit to this issue’s fork.

super_romeo’s picture

Issue summary: View changes
super_romeo’s picture

Issue summary: View changes
proweb.ua’s picture

commerce 8.x-2.29
10# not applay

super_romeo’s picture

Patch #17 works as expected.
Thank you, @Christian.wiedemann!

rossidrup’s picture

I have exactly the same error...will this be fixed? how to apply #17? it is a merged request? Where is the patch url?

rossidrup’s picture

i 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?

mile23’s picture

MR includes .idea/ PHPStorm config, which is probably not desired.

dubs’s picture

This 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.

dubs’s picture

StatusFileSize
new8.69 KB

So 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.

tBKoT made their first commit to this issue’s fork.

tbkot’s picture

Status: Needs work » Needs review
proweb.ua’s picture

#23 works
drupal 9.5.5

jsacksick’s picture

Status: Needs review » Needs work

Setting the status to "Needs work" as we really need tests coverage to ensure this doesn't break in the future again.

tbkot’s picture

Status: Needs work » Needs review
tbkot’s picture

morbus iff’s picture

#23 works for me. I've not been able to get the MR to apply.

  • jsacksick committed 4e2941b0 on 8.x-2.x authored by tBKoT
    Issue #3180541 by Christian.wiedemann, tBKoT, valic, Dubs, junaidpv,...

  • jsacksick committed f95dafd0 on 3.0.x authored by tBKoT
    Issue #3180541 by Christian.wiedemann, tBKoT, valic, Dubs, junaidpv,...
jsacksick’s picture

Status: Needs review » Fixed
Issue tags: -Needs tests

Commited the patch from the MR, thanks everyone!! Committed.

jsacksick’s picture

Status: Fixed » Needs work

A 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.

tbkot’s picture

I’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.

tbkot’s picture

One 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.

morbus iff’s picture

While #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.

tbkot’s picture

Status: Needs work » Needs review
morbus iff’s picture

@tBKoT: patch branch needs to be rebased.

mav_fly’s picture

we 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

jsacksick’s picture

@Mav_fly: Have you tried the patch from the merge request?

mav_fly’s picture

The 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.

jsacksick’s picture

@tBKoT:

I’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.

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.

jsacksick’s picture

@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?

tbkot’s picture

StatusFileSize
new20.19 KB

Patch with the latest changes in MR 83

  • jsacksick committed 236335bf on 8.x-2.x authored by tBKoT
    Issue #3180541 by tBKoT, Christian.wiedemann, valic, Dubs, junaidpv,...

  • jsacksick committed 2a978edf on 3.0.x authored by tBKoT
    Issue #3180541 by tBKoT, Christian.wiedemann, valic, Dubs, junaidpv,...
jsacksick’s picture

Status: Needs review » Fixed

@tBKot: Thank you! Committed!

  • jsacksick committed 0b2a4db4 on 8.x-2.x
    Issue #3180541 followup: Commit the missing event subscriber.
    

  • jsacksick committed 09587b41 on 3.0.x
    Issue #3180541 followup: Commit the missing event subscriber.
    
morbus iff’s picture

With this patch committed, looks like #40 is still an issue for me.

tbkot’s picture

Hi @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

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

almador’s picture

When 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.patch

I'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_product

Here is the Call stack:

Drupal\Core\Plugin\Context\ContextHandler->applyContextMapping (\home\user\site\public_html\core\lib\Drupal\Core\Plugin\Context\ContextHandler.php:150)
Drupal\block\BlockAccessControlHandler->checkAccess (\home\user\site\public_html\core\modules\block\src\BlockAccessControlHandler.php:92)
Drupal\Core\Entity\EntityAccessControlHandler->access (\home\user\site\public_html\core\lib\Drupal\Core\Entity\EntityAccessControlHandler.php:109)
Drupal\Core\Entity\EntityBase->access (\home\user\site\public_html\core\lib\Drupal\Core\Entity\EntityBase.php:329)
Drupal\block\BlockRepository->getVisibleBlocksPerRegion (\home\user\site\public_html\core\modules\block\src\BlockRepository.php:63)
Drupal\block\Plugin\DisplayVariant\BlockPageVariant->build (\home\user\site\public_html\core\modules\block\src\Plugin\DisplayVariant\BlockPageVariant.php:138)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare (\home\user\site\public_html\core\lib\Drupal\Core\Render\MainContent\HtmlRenderer.php:270)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse (\home\user\site\public_html\core\lib\Drupal\Core\Render\MainContent\HtmlRenderer.php:128)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray (\home\user\site\public_html\core\lib\Drupal\Core\EventSubscriber\MainContentViewSubscriber.php:90)
call_user_func:{/app/public_html/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111} (\home\user\site\public_html\core\lib\Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher.php:111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch (\home\user\site\public_html\core\lib\Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher.php:111)
Symfony\Component\HttpKernel\HttpKernel->handleRaw (\home\user\site\vendor\symfony\http-kernel\HttpKernel.php:186)
Symfony\Component\HttpKernel\HttpKernel->handle (\home\user\site\vendor\symfony\http-kernel\HttpKernel.php:76)
Drupal\Core\StackMiddleware\Session->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\Session.php:53)
Drupal\Core\StackMiddleware\KernelPreHandle->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\KernelPreHandle.php:48)
Drupal\Core\StackMiddleware\ContentLength->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\ContentLength.php:28)
Drupal\page_cache\StackMiddleware\PageCache->pass (\home\user\site\public_html\core\modules\page_cache\src\StackMiddleware\PageCache.php:106)
Drupal\page_cache\StackMiddleware\PageCache->handle (\home\user\site\public_html\core\modules\page_cache\src\StackMiddleware\PageCache.php:85)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\ReverseProxyMiddleware.php:48)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\NegotiationMiddleware.php:51)
Drupal\Core\StackMiddleware\AjaxPageState->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\AjaxPageState.php:36)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle (\home\user\site\public_html\core\lib\Drupal\Core\StackMiddleware\StackedHttpKernel.php:51)
Drupal\Core\DrupalKernel->handle (\home\user\site\public_html\core\lib\Drupal\Core\DrupalKernel.php:741)
{main} (\home\user\site\public_html\index.php:19)