For some reason this error sometimes appears.
Any solutions ? Drupal 8.7.8

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getCacheMaxAge() on null in Drupal\views\Plugin\views\query\Sql->getCacheMaxAge() (line 1690 of core/modules/views/src/Plugin/views/query/Sql.php).
Drupal\views\Plugin\views\query\Sql->getCacheMaxAge() (Line: 260)
Drupal\views\Plugin\views\cache\CachePluginBase->getCacheMaxAge() (Line: 677)
Drupal\views\Plugin\views\style\StylePluginBase->renderFields(Array) (Line: 576)
Drupal\views\Plugin\views\style\StylePluginBase->renderGrouping(Array, Array, 1) (Line: 468)
Drupal\views\Plugin\views\style\StylePluginBase->render(Array) (Line: 2111)
Drupal\views\Plugin\views\display\DisplayPluginBase->render() (Line: 1533)
Drupal\views\ViewExecutable->render() (Line: 183)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1630)
Drupal\views\ViewExecutable->executeDisplay('page_ei', Array) (Line: 77)
Drupal\views\Element\View::preRenderViewElement(Array)
call_user_func(Array, Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Comments

mmjvb’s picture

When line 1690:

1690       $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());

it is part of a foreach assuming it finds entities. Suggest to assign the found entities to a variable and make the foreach conditional to being an array:

1688     $max_age = parent::getCacheMaxAge();
         $entities = $this->getAllEntities();
         if (is_array($entities) {
1689        foreach ($entities as $entity) {
1690          $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
1691        }
         }

Note that doesn't calculate the max_age correctly. No idea why, you may want to trigger an exception or log that it can't find the entities.

kars4’s picture

Thank you for your help.
I tried your suggestion, but no success.

The website encountered an unexpected error. Please try again later.
ParseError: syntax error, unexpected 'foreach' (T_FOREACH) in Composer\Autoload\includeFile() (line 1691 of core/modules/views/src/Plugin/views/query/Sql.php).

I am not familiar with drupal code and log doesn't shaw which entities not found.

mmjvb’s picture

Missing closing bracket on if.

if (is_array($entities) {

should be:

if (is_array($entities)) {
kars4’s picture

Yeah bracket was missing, I added and now comes out

The website encountered an unexpected error. Please try again later.
ParseError: syntax error, unexpected 'protected' (T_PROTECTED) in Composer\Autoload\includeFile() (line 1703 of core/modules/views/src/Plugin/views/query/Sql.php).

mmjvb’s picture

That causes the function not to end, not understanding that protected is defining a new function.

kars4’s picture

You are right. Thank you very much 

kars4’s picture

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getCacheMaxAge() on null in Drupal\views\Plugin\views\query\Sql->getCacheMaxAge() (line 1692 of core/modules/views/src/Plugin/views/query/Sql.php).

I was thinking its resolved, but happening again. And this time it happen at logout.

mmjvb’s picture

1703   protected function getAllEntities() {
1704     $entities = [];
1705     foreach ($this->view->result as $row) {
1706       if ($row->_entity) {
1707         $entities[] = $row->_entity;
1708       }
1709       foreach ($row->_relationship_entities as $entity) {
1710         $entities[] = $entity;
1711       }
1712     }
1713
1714     return $entities;
1715   }

There are multiple ways to resolve this, purely based on the code I would go for:

1703   protected function getAllEntities() {
1704     $entities = [];
1705     foreach ($this->view->result as $row) {
1706       if (!empty($row->_entity)) {
1707         $entities[] = $row->_entity;
1708       }
1709       foreach ($row->_relationship_entities as $entity) {
             if (!empty($entity)) {
1710            $entities[] = $entity;
             }
1711       }
1712     }
1713
1714     return $entities;
1715   }

Added two !empty() function calls to avoid adding those entries.

You could instead make 

$max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());

conditional to !empty($entity

if (!empty($entity)) {
   $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
}

Would probably prefer this because it is not depending on other function to produce the expect result for your function.

As mentioned there probably is some data corruption in your views. Suggest inspecting and correcting them.

kars4’s picture

Both ways return same error

The website encountered an unexpected error. Please try again later.
Error: Call to a member function getCacheTags() on null in Drupal\views\Plugin\views\cache\CachePluginBase->getRowCacheTags()
(line 318 of core/modules/views/src/Plugin/views/cache/CachePluginBase.php).

kars4’s picture

By second way, I cleared all caches, seems works so far

kars4’s picture

Again, sometimes the same error comes out

Error: Call to a member function getCacheTags() on null in Drupal\views\Plugin\views\cache\CachePluginBase->getRowCacheTags() (line 318 of core/modules/views/src/Plugin/views/cache/CachePluginBase.php).

after clear all caches works fine

mmjvb’s picture

But it is not the same as for maxAge. The solution is the same though. Put a test around it.

kars4’s picture

I did the same solution for 

$tags = Cache::mergeTags($tags, $entity->getCacheTags());

works so far, let's see what will happen. Thank you so much

chancenyasulu’s picture

File : core/modules/views/src/Plugin/views/cache/CachePluginBase.php

line: 320

if(!is_null($entity)){
    $tags = Cache::mergeTags($tags, $entity->getCacheTags());
}

File: /core/modules/views/src/Plugin/views/query/Sql.php

Line: 1695

if(!is_null($entity)){
    $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
}

Sql.php file

Line: 1682

 if(!is_null($entity)){
     $tags = Cache::mergeTags($entity->getCacheTags(), $tags);
 }

ajaysinh’s picture

diff --git a/core/modules/views/src/Plugin/views/query/Sql.php b/modules/views/src/Plugin/views/query/Sql.php
index 47f229f9..1e8e4da6 100644
--- a/core/modules/views/src/Plugin/views/query/Sql.php
+++ b/core/modules/views/src/Plugin/views/query/Sql.php
@@ -1707,7 +1707,9 @@ class Sql extends QueryPluginBase {
   public function getCacheMaxAge() {
     $max_age = parent::getCacheMaxAge();
     foreach ($this->getAllEntities() as $entity) {
-      $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
+           if(!is_null($entity)){
+                   $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
+           }
     }

     return $max_age;
 

bdunphy’s picture

Thank you @ajaysinh from a year and a half ago. Running a D10.3 site and came across this issue recently. I ended up having to patch in 3 places.

I question why core would have a naked call like this. It should be more robust and only throw a warning/error message not a WSOD. Is there a place to report / query on this issue? Thanks.

hiraethmarkb’s picture

In case anyone lands on this post, as I did, from a search engine link; there is an open issue at the time of posting in the Drupal core issue queue https://www.drupal.org/project/drupal/issues/3169694.