Error in CountryCacheContext if no country is set

Created on 18 October 2024, 2 months ago

Problem/Motivation

Setting up a fresh Drupal commerce store in ddev for testing commerce_giftcard I ran into the following error when loading /admin/commerce/giftcards that simply shows a list of Gift cards via a simple view. As soon as the list has an entry, the following error appears.

I think this issue needs to be looked at logically, not from the concrete reproduction.

Seems it's caused in CountryCacheContext if no country has been set. Then this code runs into a null-pointer:

/**
   * {@inheritdoc}
   */
  public function getContext() {
    return $this->currentCountry->getCountry()->getCountryCode();
  }

Typically a country should be set by default, but for some strange reason, it isn't.

So I see two valid solutions:

  • Ensure a country is always set
  • Fall back to a viable default in the method above, if ->getCountry() returns null

What's the best way? Or both?

The website encountered an unexpected error. Try again later.

Error: Call to a member function getCountryCode() on null in Drupal\commerce\Cache\Context\CountryCacheContext->getContext() (line 44 of modules/contrib/commerce/src/Cache/Context/CountryCacheContext.php).

Drupal\Core\Cache\Context\CacheContextsManager->convertTokensToKeys() (Line: 219)
Drupal\Core\Cache\VariationCache->createCacheId() (Line: 57)
Drupal\Core\Cache\VariationCache->set() (Line: 92)
Drupal\Core\Render\RenderCache->set() (Line: 127)
Drupal\Core\Render\PlaceholderingRenderCache->set() (Line: 584)
Drupal\Core\Render\Renderer->doRender() (Line: 248)
Drupal\Core\Render\Renderer->render() (Line: 706)
Drupal\views\Plugin\views\style\StylePluginBase->renderFields() (Line: 572)
Drupal\views\Plugin\views\style\StylePluginBase->renderGrouping() (Line: 460)
Drupal\views\Plugin\views\style\StylePluginBase->render() (Line: 2177)
Drupal\views\Plugin\views\display\DisplayPluginBase->render() (Line: 1592)
Drupal\views\ViewExecutable->render() (Line: 201)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1689)
Drupal\views\ViewExecutable->executeDisplay() (Line: 81)
Drupal\views\Element\View::preRenderViewElement()
call_user_func_array() (Line: 113)
Drupal\Core\Render\Renderer->doTrustedCallback() (Line: 870)
Drupal\Core\Render\Renderer->doCallback() (Line: 432)
Drupal\Core\Render\Renderer->doRender() (Line: 248)
Drupal\Core\Render\Renderer->render() (Line: 238)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 638)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 231)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare() (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse() (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray()
call_user_func() (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch() (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 741)
Drupal\Core\DrupalKernel->handle() (Line: 19)

I found a similar issue here: #3095413: Error: Call to a member function getCountryCode() on null in Drupal\commerce_order\AddressBook->isAvailable() but don't think it's closely related.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

3.0

Component

Other

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024