Do not assume that a file with the given target_id exists

Created on 11 April 2024, 3 months ago
Updated 17 April 2024, 2 months ago

Problem/Motivation

My team needs to re-save thousands of entities (we're updating a certaion field's value). But, unfortunately, we have a missing file in one of the entities, which is still referenced in the active value of a field managed by File URL. Because of this, when FieldableEntityInterface::postSave() invokes FileUrlFieldItemList::postSave(), and exception is thrown:

Error: Call to a member function id() on null in /var/www/html/web/modules/contrib/file_url/src/Plugin/Field/FieldType/FileUrlFieldItemList.php on line 98
#0 [internal function]: Drupal\file_url\Plugin\Field\FieldType\FileUrlFieldItemList->postSave(true)
#1 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(939): call_user_func_array(Array, Array)
#2 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(985): Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldMethod('postSave', Object(Drupal\node\Entity\Node), true)
#3 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(897): Drupal\Core\Entity\ContentEntityStorageBase->invokeFieldPostSave(Object(Drupal\node\Entity\Node), true)
#4 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(564): Drupal\Core\Entity\ContentEntityStorageBase->invokeHook('update', Object(Drupal\node\Entity\Node))
#5 /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php(781): Drupal\Core\Entity\EntityStorageBase->doPostSave(Object(Drupal\node\Entity\Node), true)
#6 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(489): Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object(Drupal\node\Entity\Node), true)
#7 /var/www/html/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save(Object(Drupal\node\Entity\Node))
#8 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\node\Entity\Node))
...

Steps to reproduce

  1. Have an entity with a file URL field with an item referring a missing local file.
  2. Load, then try to save the entity programmatically.

Proposed resolution

  1. Adjust FileUrlFieldItemList::referencedEntities() so that it does not return any item with NULL value
  2. Adjust FileUrlFieldItemList::postSave() to not trigger any exception if a particular reference is invalid.
  3. Keep items with invalid value.
  4. (TBD) decide what should happen with the file usage record of a missing local file.

Remaining tasks

Decide what should happen with the file usage record of a missing local file.

User interface changes

Nothing.

API changes

FileUrlFieldItemList::referencedEntities() follows interface contract.

Data model changes

Nothing.

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡­πŸ‡ΊHungary huzooka Hungary πŸ‡­πŸ‡ΊπŸ‡ͺπŸ‡Ί

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