Errors generating a thumbnail if certain entity fields aren't set

Created on 12 October 2023, about 1 year ago
Updated 11 June 2024, 6 months ago

Problem/Motivation

When generating a PDF thumbnail from an entity directly, MediaPdfThumbnailImageManager::getPdfImage can throw an uncaught exception, and MediaPdfThumbnailImageManager::createPdfImageEntity can create an invalid pdf_image_entity entity.

Steps to reproduce

Create a new media entity with the expected file field filled out (below this is 'field_media_file'). Without saving the entity try to generate the thumbnail.
\Drupal::service('media_pdf_thumbnail.image.manager')->createThumbnail($entity, 'field_media_file', 'jpg', 1)
This code will throw an uncaught exception:
Drupal\Core\Database\InvalidQueryException: Query condition 'pdf_image_entity.referenced_entity_revision_id IN ()' cannot be empty. in Drupal\Core\Database\Query\Condition->condition() (line 117 of /opt/drupal/web/core/lib/Drupal/Core/Database/Query/Condition.php).

This is because $entity->getLoadedRevisionId() returns NULL unless the entity has been loaded, or had its loadedRevisionId attribute set.

If this issue is worked around in some way, such as saving the entity and then using $entity->getRevisionId(), createPdfImageEntity still has issues if $entity or $fileEntity haven't been saved yet as $entity->id() or $fileEntity->id() return NULL. This will then create a pdf_image_entity with no value for those fields, and so the entity will never match anything.

🐛 Bug report
Status

Closed: works as designed

Version

6.0

Component

Code

Created by

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

Comments & Activities

Production build 0.71.5 2024