MediaThumbnailManager->getFileObject can fail with an AssertionError: Cannot load the "file" entity with NULL ID

Created on 22 July 2025, 24 days ago

Problem/Motivation

There are conditions where the thumbnail field will have a target id set to null. Not sure how this happens but suspect it's related to the physical file deleted but the file_managed entry still there and the pdf automatic thumbnail generator adding the invalid entry.

This generally causes issues when deleting a media entity with an invalid thumbnail.

Traceback of relevant error:

#0 /app/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(261): assert(false, 'Cannot load the...')
#1 /app/docroot/modules/contrib/media_thumbnails/src/Plugin/MediaThumbnailManager.php(273): Drupal\Core\Entity\EntityStorageBase->load(NULL)
#2 /app/docroot/modules/contrib/media_thumbnails/src/Plugin/MediaThumbnailManager.php(236): Drupal\media_thumbnails\Plugin\MediaThumbnailManager->getFileObject(Object(Drupal\media\Entity\Media), 'thumbnail')

Steps to reproduce

Sorry not sure how to reproduce, except it occurs on media deletion.

Proposed resolution

Add a test to make sure that the $fid is not null before trying to load the file entity.

Remaining tasks

Create patch and upload it.

User interface changes

API changes

Data model changes

📌 Task
Status

Active

Version

2.0

Component

Code

Created by

🇺🇸United States cgmonroe

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

Merge Requests

Comments & Activities

  • Issue created by @cgmonroe
  • 🇺🇸United States cgmonroe

    Here's the patch

  • 🇮🇳India divyansh.gupta Jaipur

    Working on it!!

  • 🇮🇳India divyansh.gupta Jaipur

    I’ve been trying to reproduce this issue but wasn't able to.
    Here’s what I did:

    • Created a media item (video) with a thumbnail.
    • Manually set the thumbnail__target_id field to NULL directly in the database.
    • Cleared the cache and then tried editing and deleting that media item — everything worked fine, no errors showed up.
    • So far, I haven’t been able to trigger the error (InvalidArgumentException: Field is unknown). Maybe this has already been fixed in recent updates, or I might be missing a specific step.

    If anyone has more detailed steps to reproduce this issue, I’d really appreciate it!
    Thanks!

  • 🇺🇸United States cgmonroe

    Hmm wondering if it might be version related?

    My environment:

    Drupal version : 10.5.1
    DB driver : mysql
    PHP OS : Linux
    PHP version : 8.2.27

    Here's what the devel tab shows about the media's thumbnail that fails:

  • Pipeline finished with Success
    23 days ago
    Total: 129s
    #555596
  • 🇮🇳India divyansh.gupta Jaipur

    I’ve uploaded a merge request for review.
    The MR adds a safeguard to avoid calling load(NULL) when $fid is not set.
    While I couldn’t reproduce the issue locally, the patch avoids a fatal error in such cases without introducing side effects.
    Please review!!

Production build 0.71.5 2024