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.
- Pages on a Drupal site, particularly those being migrated from Drupal 7, can cause a WSOD for what is effectively a very minor issue.
- There is no way to see what the issue is until you enable PHP errors to screen or go check Drupal watchdog.
- 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."
- 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