Problem/Motivation
/admin/content view is not working - fatal error.
Dblog reports 3 different errors:
Error: Call to a member function getCacheTags() on null in Drupal\views\Plugin\views\query\Sql->getCacheTags() (Line 1677 in /app/web/core/modules/views/src/Plugin/views/query/Sql.php)
Error: Call to a member function getCacheMaxAge() on null in Drupal\views\Plugin\views\query\Sql->getCacheMaxAge() (Line 1694 in /app/web/core/modules/views/src/Plugin/views/query/Sql.php)
Error: Call to a member function getCacheTags() on null in Drupal\views\Plugin\views\cache\CachePluginBase->getRowCacheTags() (Zeile 320 in /app/web/core/modules/views/src/Plugin/views/cache/CachePluginBase.php)
The issue lies in web/core/modules/views/src/Plugin/views/query/Sql.php in getAllEntities(line 1707) method, where $row->_relationship_entities can be NULL but are not checked if emtyp (as $row->_entity is).
Steps to reproduce
It is related to our content so I'm not sure how exactly to reproduce. On some of our migrated nodes an entity reference field is missing.
Proposed resolution
I would actually put a check into the 3 methods that are throwing php errors to check if the entity is not empty and then perform caching operations (merging tags or getRowCacheTags).
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | get_cache_tags_null-3169694-2.patch | 1.56 KB | useernamee |
Issue fork drupal-3169694
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
Comment #2
useernamee commentedI'm attaching proposed patch.
Comment #3
useernamee commentedComment #4
fagoComment #5
fagoComment #6
useernamee commentedComment #7
gatorjoe commentedI deleted the group relationship for a piece of content and started receiving an error. Patch #2 resolved my issue. For reference here is the corresponding log message:
Side note, incredibly happy to encounter an error and find a solution that was posted only hours before! :) Thank you!!!
Comment #8
lendudeUsually this type of change just hides the underlying problem. Nice to get things working again on a broken system but not something we want to do al the time.
Without steps to reproduce on a clean install, it becomes hard to see if this is a situation that you can get into without having broken content.
Comment #9
mikelutzFor what it's worth, I came across this error and it was due to having the multiversion module installed. Multiversion adds a soft delete to nodes, where they stay in the node table, but adds a _deleted flag to the table. Views gets it's list of entity ids through raw sql queries that don't filter out these deleted nodes, but when it goes to load them, it loads them through multiversion's overridden entity storage that won't load the deleted entities. So you end up with views thinking it has entities to loop through, but they are NULL because of multiversion.
In this case, you can add a filter to your view that filters out rows with the deleted flag, this will fix your views, because views will filter those entities out at the sql level, and then there won't be any entity ids to load that don't actually exist.
Comment #10
useernamee commented@mikelutz thanks for the explanation! It makes sense. Are you suggesting that this is actually something multiversion should fix (or warn about at least)?
Comment #11
lendudeMoving this to the Drupal core version currently receiving bug fixes.
I get this error a lot too, but it is always due to the problem pointed out in #7 were I messed up my group relationships (probably by manually deleting stuff from my local DB, or half reverting part of a migration)
I'm going to tentatively mark this 'works as designed' because it seems to be related to having corrupt data in your database or being caused by contrib. Feel free to reopen this if you have some steps to reproduce this on a vanilla install or if you feel like this is a situation we should handle in core.
Comment #12
zcht commentedUnfortunately I have the same problem, but this patch does not help. I noticed it when I enable the metatag views submodule and am on a search view. I have listed the issue here: https://www.drupal.org/project/metatag/issues/3245876
See also observations listed in my second comment, is this possibly the problem?
What would I need to adjust to fix this issue for my search view? Many thanks.
Comment #13
zcht commentedComment #14
lendude@zcht the assert messages should be unrelated to this. As pointed out in #11, this error is usually due to missing/malformed/corrupt data in your database or a contrib module doing something that it probably shouldn't be doing.
Feel free to reopen this if there is some way to reproduce this with a clean Drupal core install, I'm still unsure if this is something we can or should guard against in core.
Comment #15
studgate commentedthis also works for Drupal 9.4.5
Comment #16
maskedjellybeanI ran into this error as well, but decided to figure out what the root cause was instead of applying the patch. I can confirm that this is caused by corrupted data as @Lendude said.
In my case there were nodes that failed to load.
getCacheTags()is then called on null instead of the node entity resulting in the error. You could argue that core should check for this, but I think there is an assumption that a node should never fail to load, and that if it does you probably want to know about it so that you can fix it.What I found is that there were nodes that existed in the
node_field_datatable that did not exist innode. This should never happen.Here's the query I ran to determine that:
That gave me a list of 7 corrupted nodes. I then wrote a hook_update_N() that loops through the nodes, recreates them and deletes them. Here's what MY update hook looked like, but DO NOT RUN THIS AS IS. You need to customize it for your situation. Yours may be more or less complicated than mine. You need to change "NID_HERE" to the nids and content types of YOUR corrupted nodes. ALWAYS test update hooks locally multiple times before running on production.
Comment #17
chancenyasulu commentedCan we change this parts
if (empty($entity)) {
+ continue;
+ }
by using !is_null and instead of continue; replace it with these lines
first part: $tags = Cache::mergeTags($tags, $entity->getCacheTags());
second part: $tags = Cache::mergeTags($entity->getCacheTags(), $tags);
third part: $max_age = Cache::mergeMaxAges($max_age, $entity->getCacheMaxAge());
if(!is_null($entity)){
$tags = Cache::mergeTags($entity->getCacheTags(), $tags);
}
Comment #18
pganore1@gmail.com commentedAny idea, when will this be merged into Drupal 10 ?
Comment #19
timefor commentedI encountered this exact error. Turned out it was due to a corrupted row in the 'users' table of the database. The issue arose when I transferred data between environments: MySQL failed to create the anonymous user with UID=0, instead assigning the next available UID. This newly assigned UID had no corresponding entry in the 'users_data' table, causing disruptions in views loading content associated with the anonymous user.
Though the above patch helped identify the root cause, it wasn't a permanent fix. To resolve the problem, I manually set the UID for the anonymous user to 0 in the database, eliminating the need for the patch.
I used Navicat MySQL for data transfer, but I'm uncertain if that's where the UID discrepancy began, or if it might be related to some default setting within the SQL database that prevents assignment of a UID of 0.
I attempted to run the process in #16, but I had no corrupt nodes so it wasn't the problem. Anyway, just something else to try for the next person tracking down this views error.
Comment #20
robcarr@TimeFor - thanks for solution. I'd seen the error and it was exactly that issue where I'd transferred databases between environments. Worth noting as a potential Drupal general problem.
It was a case of looking in users_field_data table and comparing UIDs to the users table and seeing where there was a mismatch. In my case it was the highest UID in the users table that was erroneous - just edited the users table in PMA and altered the relevant UID value to 0
Comment #21
sadikyalcin commented@TimeFor what a life saver. That was exactly the issue for me. I moved to a new db and watchdog kept breaking with cache errors.
Error: Call to a member function getCacheTags()
Error: Call to a member function getCacheMaxAge()
Comment #22
aniketd commented#2 patched code works for the Drupal 11.1.1
Comment #23
cilefen commentedBased on #3514713: Fatal error "getCacheTags() on null" on admin/content Views page being opened I am reopening this. It still needs steps to reproduce. #3514713: Fatal error "getCacheTags() on null" on admin/content Views page doesn't have any.
Comment #24
quietone commentedDrupal 9 is End of Life.
Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies.
Comment #25
alphex commentedPatch in comment #2 worked on 10.4.6
Comment #26
anaconda777 commentedI got the same error on Drupal 10.4.7
It happened after deleting some nodes meanwhile ECA was analyzing the nodes and setting their moderation states etc. Maybe this was not the reason.
Anyway like before someone instructed I managed to fix it and deleted the corrupted node like this:
Comment #27
afeijoI got this error for months specially at the Reports page, and sometimes at the Content page.
Scary to se a 2020 patch still not commited to drupal
#2 patch works, thanks! I'm using version 10.4.6 too btw
Comment #28
brunoalmeida commentedPatch #2 worked on 10.5.1
Comment #29
mikelutzComment #30
bdunphy commented#2 also prevents a WSOD. Checking that a variable contains / does not contain a value is good programming practice. One could take the patch and add in a logger message. Something to warn the site maintainer that they need to check the data integrity.
Comment #31
millenniumtree@mikelutz
Please listen to the consensus here. This type of thinking (bad data, no issue!) is why Drupal objects feel so fragile. If a simple if() can prevent a site bombing, then please implement it! The error being produced doesn't tell you which object is at fault, or how to correct it, it's just a WSOD. It's absolutely unacceptable that this issue gets reported, acknowledged, and patched by the community, but IGNORED by the Drupal team, for coming up on FIVE YEARS.
Comment #32
anaconda777 commentedI got this error again, it was because a bad ECA model, which deleted a node
Comment #33
lazzyvn commentedI got this error when i go to /admin/reports/dblog
apply this patch it works
Comment #34
frobWhile it would be nice for this to be fixed by fixing the content, it doesn't change the fact that this needs to be fixed on output as well.
A "better" fix would be for the error to happen when the content is corrupted. This isn't something that is happening when users are editing the DB directly. It is happening when entities are being deleted. I wish I had concrete example of how this is happening. The closest I have gotten is when a WSOD gets logged dealing with user data for an Anonymous user. That breaks dblog page. But I don't see that as the error here.
The bug this fixes is that views is trying to calculate cache on entities that don't exist.
Comment #35
frobApplying this patch fixes the issue of views breaking when the calculated referenced entities don't exist. I might make a new issue to deal with the Anonymous user bugs if I can get time to replicated it.
Comment #36
adrianm6254 commentedI applied patch #2 and it fixed my 'Recent log messages' going WSOD
Comment #37
catchThis needs to be an MR so that tests run etc., it could also probably use explicit test coverage.
Comment #39
mayurgajar commentedComment #41
frobDoes this need work? Looks like the MR test failed on lint.
Comment #42
mayurgajar commentedAdded MR: https://git.drupalcode.org/project/drupal/-/merge_requests/13934
Steps to reproduced:
1. Migrate content from site
2. the issue occurs to the migrated content
3. check the nodes / user data
4. user encounter issue: Error: Call to a member function getCacheTags() on null
Apply the MR and check again
MR raised against 11.x
Comment #43
smustgrave commented#37 still applies
Comment #45
jonathan_hunt commentedfwiw, patch in #2 still applies to 10.6.5. However, I think rather than silently ignoring data errors such as dangling references they should be logged so they can come to the attention of an administrator.
Given that Drupal core has an issue acknowledging that dangling references are not cleaned up https://www.drupal.org/project/drupal/issues/2723323 I think it's important for UX not to WSOD when such a reference is encountered.
Comment #46
jonathan_hunt commentedComment #50
sivaji_ganesh_jojodae commentedI've updated the previous merge request after rebasing. The pipeline now appears to be successful.
Comment #51
smustgrave commentedMost issues need more then rebasing. Since steps still have not been provided moving back.