Product Variation is without context in view based listing of Products when using Layout builder for managing product display

Created on 3 November 2020, about 4 years ago
Updated 7 August 2024, 5 months ago

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

🐛 Bug report
Status

Fixed

Version

2.0

Component

Product

Created by

🇭🇷Croatia valic Osijek

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom Dubs

    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.

  • 🇬🇧United Kingdom Dubs

    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.

  • 🇬🇧United Kingdom Dubs

    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.

  • First commit to issue fork.
  • Status changed to Needs review almost 2 years ago
  • 🇺🇦Ukraine proweb.ua

    #23 works
    drupal 9.5.5

  • Status changed to Needs work almost 2 years ago
  • 🇮🇱Israel jsacksick

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

  • Status changed to Needs review almost 2 years ago
  • 🇺🇸United States morbus iff

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

  • Status changed to Fixed over 1 year ago
  • 🇮🇱Israel jsacksick

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

  • Status changed to Needs work over 1 year ago
  • 🇮🇱Israel jsacksick

    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.

  • 🇺🇦Ukraine 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.

  • 🇺🇦Ukraine tbkot

    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.

  • 🇺🇸United States morbus iff

    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. Now, if I make a view called "Similar Products", intended to be displayed on product A, then 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, 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.

  • Status changed to Needs review over 1 year ago
  • 🇺🇸United States morbus iff

    @tBKoT: patch branch needs to be rebased.

  • Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    Not currently mergeable.
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    780 pass, 2 fail
  • Pipeline finished with Canceled
    about 1 year ago
    #53335
  • Pipeline finished with Failed
    about 1 year ago
    #53337
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    780 pass, 2 fail
  • Pipeline finished with Failed
    about 1 year ago
    #53348
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    780 pass, 2 fail
  • Pipeline finished with Failed
    about 1 year ago
    #53856
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    638 pass, 67 fail
  • Pipeline finished with Skipped
    about 1 year ago
    #53896
  • Pipeline finished with Skipped
    about 1 year ago
    #53897
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    638 pass, 67 fail
  • Pipeline finished with Failed
    about 1 year ago
    #53894
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    638 pass, 67 fail
  • Pipeline finished with Failed
    about 1 year ago
    #53900
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    780 pass, 1 fail
  • Pipeline finished with Failed
    about 1 year ago
    #53908
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    787 pass
  • Pipeline finished with Success
    about 1 year ago
    #53942
  • 🇮🇱Israel jsacksick

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

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    750 pass, 10 fail
  • Pipeline finished with Canceled
    11 months ago
    #81466
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    750 pass, 10 fail
  • Pipeline finished with Canceled
    11 months ago
    #81468
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    750 pass, 10 fail
  • Pipeline finished with Failed
    11 months ago
    #81469
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    783 pass, 1 fail
  • Pipeline finished with Running
    11 months ago
    #81534
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    790 pass
  • Pipeline finished with Success
    11 months ago
    #81542
  • 🇮🇱Israel jsacksick

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

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    791 pass
  • Pipeline finished with Failed
    11 months ago
    Total: 467s
    #87752
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    784 pass, 2 fail
  • Pipeline finished with Success
    11 months ago
    Total: 458s
    #87773
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    791 pass
  • 🇺🇦Ukraine tbkot

    Patch with the latest changes in MR 83

  • Status changed to Fixed 11 months ago
  • 🇮🇱Israel jsacksick

    @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.
      
  • 🇺🇸United States morbus iff

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

  • 🇺🇦Ukraine tbkot

    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

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

  • 🇬🇪Georgia almador

    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)
    
Production build 0.71.5 2024