Normalizing file entity with missing file throws error

Created on 7 May 2024, about 2 months ago
Updated 15 June 2024, 11 days ago

Problem/Motivation

I am using the default_content_deploy (DCD) module to deploy content across different environments. DCD leverages the file_entity module to handle the export and import of file entities across different Drupal installations. During this process, file entities are normalized to include their data directly within the exported JSON, using the FileEntityNormalizer class provided by file_entity.

When attempting to normalize file entities where the associated files have been removed from the filesystem (but the file entity still exists in the database), the normalize() function in FileEntityNormalizer triggers a warning and fails because it attempts to read non-existent files using file_get_contents(). This can interrupt the batch process used in content deployment, leading to incomplete exports and potential data integrity issues.

Steps to reproduce

  • Create a file entity and upload a file to the Drupal site.
  • Delete the file from the filesystem without removing the corresponding file entity from Drupal.
  • Run the export process using default_content_deploy, or any other method that would trigger the normalize() method on that file entity.
  • Observe the following error(s)

Warning: file_get_contents(public://2024-05/test.png): Failed to open stream: "Drupal\Core\StreamWrapper\PublicStream::stream_open" call failed in /var/www/html/dv20.openplus.ca/html/modules/contrib/file_entity/src/Normalizer/FileEntityNormalizer.php on line 26

The website encountered an unexpected error. Try again later.TypeError: Unsupported operand types: null + array in Drupal\file_entity\Normalizer\FileItemNormalizer->normalize() (line 45 of modules/contrib/file_entity/src/Normalizer/FileItemNormalizer.php). Symfony\Component\Serializer\Serializer->normalize(Object, 'hal_json', Array) (Line: 68)

Proposed resolution

Implement a file existence check before attempting to read the file data in the normalize() method of the FileEntityNormalizer. This will prevent errors and allow the batch process to continue, even if some files are missing.

🐛 Bug report
Status

Fixed

Component

Code

Created by

🇨🇦Canada smulvih2 Canada 🍁

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024