Call to a member function getCacheTags() on null

Created on 3 March 2018, over 6 years ago
Updated 15 September 2023, about 1 year ago

Problem/Motivation

Various scenarios can cause a fatal error and white-screen-of-death (WSOD). The underlying error message is:

Error: Call to a member function getCacheTags() on null in Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter->viewElements() (line 199 of /home/public_html/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php)
#0 /home/public_html/core/lib/Drupal/Core/Field/FormatterBase.php(89): Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter->viewElements(Object(Drupal\file\Plugin\Field\FieldType\FileFieldItemList), 'en')
#1 

The root cause is a missing image style defined somewhere in your configuration, which can be exceedingly hard to find without enabling Xdebug, setting a breakpoint, and stepping through the code.

Once the offending (missing) image style machine name is uncovered, the fix is fairly trivial, to manually go create and configure the missing image style.

This is a frustrating Developer Experience and Site Builder issue.

  1. Pages on a Drupal site, particularly those being migrated from Drupal 7, can cause a WSOD for what is effectively a very minor issue.
  2. There is no way to see what the issue is until you enable PHP errors to screen or go check Drupal watchdog.
  3. The error message, once you find it, is less than helpful (even with stack trace on) and without any indication of which entity config or image style broke the siteโ€”to quote one developerโ€” "is like searching a needle in the haystack. With hundreds of image fields this takes ages to narrow down."
  4. This situation is particularly nasty when Drupal 7's image styles contain hyphens which Drupal 8, 9, and 10 prevent creating machine names with hyphens through the UI.

Steps to reproduce

TBD.

Proposed resolution

  • Handle the error, don't cause a fatal exception, and alert the site builder/developer of the broken image style that triggered the Exception both in logs and an on-screen message.
  • Consider creating the image style on the fly to prevent more errors in logs, and alert the user on how to configure it with a link.
  • Also, tell the user what entity and field configuration triggered the error, and provide a link to the field definition to go remediate.

Remaining tasks

  • Consider creating the image style on the fly to prevent more errors in logs, and alert the user on how to configure it with a link.
  • Also, tell the user what entity and field configuration triggered the error, and provide a link to the field definition to go remediate.

User interface changes

User will be warned via a Drupal system message when this error occurs, instead of seeing a WSOD.

API changes

Not applicable.

Data model changes

Not applicable.

Original report by @nirshemer

I am running Drupal 8.4.5 (Apache 2.2 / MySQL 5.5.57 / PHP 5.6.31) and when I try to run Cron from the Admin -> Configuration -> System -> Cron (admin/config/system/cron) I am getting the following PHP Fatal Error:

Fatal error: Call to a member function getCacheTags() on null in /var/www/html/nursing/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatter.php on line 199
๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Image moduleย  โ†’

Last updated 10 days ago

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States nirshemer

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

Production build 0.71.5 2024