WSOD Argument #1 ($view_mode) must be of type string, null given

Created on 26 January 2024, 10 months ago
Updated 27 April 2024, 7 months ago

Problem/Motivation

On my site, the module produced a WSOD. The error was:

TypeError: view_mode_switch_entity_view_mode_alter(): Argument #1 ($view_mode) must be of type string, null given, called in /var/www/web/core/lib/Drupal/Core/Extension/ModuleHandler.php on line 545 in view_mode_switch_entity_view_mode_alter() (line 27 of modules/contrib/view_mode_switch/view_mode_switch.module).

This was triggered on block content. It appears that sometimes the $view_mode argument is null, and the type hint will not allow null.

Steps to reproduce

Install the module on Drupal 10.2.
Clear caches.

Proposed resolution

Remove type hint and handle null case.

πŸ› Bug report
Status

Closed: works as designed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States earthday47 New York

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

Comments & Activities

  • Issue created by @earthday47
  • πŸ‡©πŸ‡ͺGermany hctom

    Thanks for the report. This is a strange problem, because core's hook_entity_view_mode_alter documentation says, that it MUST be a string. I am also using this module in combination with custom block types and I have never experienced this issue so far.

    Do you have some more information (e.g. if it is an inline block in layout builder or a normal custom block, list any other modules that may interfere here in your setup) for me, that helps to get an understanding of this issue?

  • πŸ‡ΊπŸ‡ΈUnited States earthday47 New York

    Here's some more details I can provide -

    • The error was triggered on entity of type "block_content"
    • It is a custom block type with only a body field. There were no customizations made to any other view modes so this would be trying to use the 'default' view mode.
    • This site, and this block content, was upgraded from Drupal 8->9->10.

    So it's possible this is a migration issue?

  • πŸ‡ΊπŸ‡ΈUnited States earthday47 New York

    Here's my patch attempt.

  • Status changed to Needs review 10 months ago
  • πŸ‡ΊπŸ‡ΈUnited States earthday47 New York
  • Status changed to Postponed: needs info 10 months ago
  • πŸ‡©πŸ‡ͺGermany hctom

    To be honest, I also think that this may have something to do with the migration. I double checked the core code invoking this alter hook (in EntityViewBuilder::getBuildDefaults()) and everywhere only string are used and all calling methods use full as fallback value for their $view_mode arguments. So either there is some code that tries to render blocks programmatically with an explicitly set NULL value for $view_mode when calling ANY_ENTITY_LIST_BUILDER::viewMultiple() or your migrated blocks are somehow causing this issue (which is really hard to identify from far away).

    So please use your patch for now and I am setting the issue's priority back to "Normal" and status to "Needs more info", because the code itself follows core's argument value type.

    If you have any other information that might help here, feel free to update this issue.

  • πŸ‡ΊπŸ‡ΈUnited States earthday47 New York

    I checked and the block is placed using the default block layout. But the patch will work for in the short term.
    If I have more information I'll definitely update! Thanks for your quick responses!

  • πŸ‡©πŸ‡ͺGermany hctom

    Hi @earthday47, I just wanted to know, if you found something that caused this issue or have any new information for me? If so, feel free to re-open or update this ticket, please. Thanks!

  • Status changed to Closed: works as designed 7 months ago
  • πŸ‡©πŸ‡ͺGermany hctom

    I am closing this ticket now, because there was no feedback anymore. Feel free to open a new issue, if there is still an issue with this module.

Production build 0.71.5 2024