Error: Call to a member function getEntityTypeId()

Created on 26 October 2023, about 1 year ago
Updated 14 June 2024, 6 months ago

Problem/Motivation

Error: Call to a member function getEntityTypeId() on null in Drupal\notification_message\Cache\Context\RouteEntityUuidCacheContext->entityCacheTagsFromRoute() (line 84 of modules/contrib/notification_message/src/Cache/Context/RouteEntityUuidCacheContext.php).
Drupal\notification_message\Cache\Context\RouteEntityUuidCacheContext->getContext(NULL) (Line: 123)
Drupal\Core\Cache\Context\CacheContextsManager->convertTokensToKeys(Array) (Line: 317)
Drupal\Core\Render\RenderCache->createCacheID(Array) (Line: 66)
Drupal\Core\Render\RenderCache->get(Array) (Line: 109)
Drupal\Core\Render\PlaceholderingRenderCache->get(Array) (Line: 273)

Steps to reproduce

Essentially the current function assumes that a contextual filter is going to be an ID, and a proper entity slug. The way to reproduce this is to really configure any other type of contextual filter. For our particular configuration:

  • Create a new view with a page display and a path of anything/%taxonomy_term
  • Create a new contextual filter which is the Term Name. This works normally with the taxonomy term validator.

Proposed resolution

There's not any super straightforward way to solve this. If you don't use an entity slug, and you just use '%', then it will fail on getStorage('arg_0').
If you setup the contextual filter to be a taxonomy term name, then the call to getStorage('taxonomy_term')->load('name of taxonomy term') will fail.

The best we can do is first do a hasDefinition() to see if the storage actually exists and if it's an actual entity type. Then attempt to load the entity and just continue if it fails.

Remaining tasks

Review and test included patch.

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

🇺🇸United States asherry

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

Merge Requests

Comments & Activities

  • Issue created by @asherry
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update about 1 year ago
    3 pass
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update about 1 year ago
    3 pass
  • 🇺🇸United States asherry

    Ran into another error if the $arg variable turns out to be a proper EntityInterface object. I put in another check to see if it's a proper entity, just add it to the array, but, also check if it's an int/str to make sure it's safe to pass to entity load.

    Still not sure about this solution as a whole, but, it's fixing errors for us at the moment.

  • 🇫🇮Finland heikkiy Oulu

    I found this issue when installing Notification message to our side crashed a view with contextual filters in the URL.

    The error in our case was this

    The website encountered an unexpected error. Try again later.
    
    Drupal\Component\Plugin\Exception\PluginNotFoundException: The "arg_0" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 139 of core/lib/Drupal/Core/Entity/EntityTypeManager.php).
    
    Drupal\Core\Entity\EntityTypeManager->getHandler() (Line: 192)
    Drupal\Core\Entity\EntityTypeManager->getStorage() (Line: 129)
    Drupal\notification_message\Cache\Context\RouteEntityUuidCacheContext->attachViewRouteEntities() (Line: 86)
    Drupal\notification_message\Cache\Context\RouteEntityUuidCacheContext->entityCacheTagsFromRoute() (Line: 60)
    Drupal\notification_message\Cache\Context\RouteEntityUuidCacheContext->getContext() (Line: 123)
    Drupal\Core\Cache\Context\CacheContextsManager->convertTokensToKeys() (Line: 244)
    Drupal\Core\Cache\VariationCache->createCacheIdFast() (Line: 175)
    Drupal\Core\Cache\VariationCache->getRedirectChain() (Line: 35)
    Drupal\Core\Cache\VariationCache->get() (Line: 71)
    Drupal\Core\Render\RenderCache->get() (Line: 113)
    Drupal\Core\Render\PlaceholderingRenderCache->get() (Line: 317)
    Drupal\Core\Render\Renderer->doRender() (Line: 493)
    Drupal\Core\Render\Renderer->doRender() (Line: 240)
    Drupal\Core\Render\Renderer->render() (Line: 475)
    Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 122)
    __TwigTemplate_a80a95a6a8c5a3904dde60ae71cea77e->block_page_pre_content() (Line: 171)
    Twig\Template->displayBlock() (Line: 130)
    __TwigTemplate_f0b5e2f7c622ad70fb53191694409337->doDisplay() (Line: 394)
    Twig\Template->displayWithErrorHandling() (Line: 367)
    Twig\Template->display() (Line: 61)
    __TwigTemplate_a80a95a6a8c5a3904dde60ae71cea77e->doDisplay() (Line: 394)
    Twig\Template->displayWithErrorHandling() (Line: 367)
    Twig\Template->display() (Line: 39)
    __TwigTemplate_9e59da2f5f2329765a9dfd034cd52843->doDisplay() (Line: 394)
    Twig\Template->displayWithErrorHandling() (Line: 367)
    Twig\Template->display() (Line: 379)
    Twig\Template->render() (Line: 38)
    Twig\TemplateWrapper->render() (Line: 39)
    twig_render_template() (Line: 348)
    Drupal\Core\Theme\ThemeManager->render() (Line: 480)
    Drupal\Core\Render\Renderer->doRender() (Line: 240)
    Drupal\Core\Render\Renderer->render() (Line: 475)
    Drupal\Core\Template\TwigExtension->escapeFilter() (Line: 81)
    __TwigTemplate_cba7f8237d6b6745d5fea617efe7d916->doDisplay() (Line: 394)
    Twig\Template->displayWithErrorHandling() (Line: 367)
    Twig\Template->display() (Line: 39)
    __TwigTemplate_f020d8f0b6dad9d1f81515d1f59f880c->doDisplay() (Line: 394)
    Twig\Template->displayWithErrorHandling() (Line: 367)
    Twig\Template->display() (Line: 379)
    Twig\Template->render() (Line: 38)
    Twig\TemplateWrapper->render() (Line: 39)
    twig_render_template() (Line: 348)
    Drupal\Core\Theme\ThemeManager->render() (Line: 480)
    Drupal\Core\Render\Renderer->doRender() (Line: 240)
    Drupal\Core\Render\Renderer->render() (Line: 158)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 627)
    Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 153)
    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: 58)
    Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
    Drupal\Core\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: 704)
    Drupal\Core\DrupalKernel->handle() (Line: 19)
    

    The patch in this issue fixed the problem. I would be willing to mark this RTBC and mark it Major or even Critical because it causes a crash. I will see if I can come up with another suggestion how to fix this.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 6 months ago
    3 pass
  • 🇫🇮Finland heikkiy Oulu

    I tried to improve the code a bit and made some small improvements. Here is an attached patch with following changes.

    1. Added a check for $route_object being null to avoid potential errors.
    2. Used instanceof instead of is_a for checking if $arg is an instance of EntityInterface.
    3. Some small style changes.

    Tested that the issue is still resolved.

  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 6 months ago
    3 pass
  • 🇫🇮Finland heikkiy Oulu

    I opened a MR for this for easier review and commenting.

Production build 0.71.5 2024