file_get_file_references assumes computed fields have storage

Created on 19 September 2023, 9 months ago
Updated 11 October 2023, 8 months ago

Problem/Motivation

Function file_get_file_references assumes that all fields have a storage definition. But that's not true for some computed fields.

The call to ->getType here can happen on a NULL variable.

Steps to reproduce

1. Install a standard profile
2. Edit user 1, set a picture. This will create a file entity with ID 1.
3. Add the following to a custom module and enable the module. You'll see the error.


use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\file\Entity\File;

/**
 * Implements hook_entity_base_field_info().
 */
function atest_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type->id() === 'user') {
    $fields['test_picture'] = BaseFieldDefinition::create('image')
      ->setSetting('target_type', 'file')
      ->setLabel('Test picture')
      ->setCustomStorage(TRUE)
      ->setComputed(TRUE)
      ->setReadOnly(TRUE)
      ->setDisplayConfigurable('view', TRUE);
  }
  return $fields;
}

/**
 * Implements hook_ENTITY_TYPE_storage_load().
 */
function atest_user_storage_load(array $users) {
  foreach ($users as $user) {
    $user->set('test_picture', 1);
  }
}

/**
 * Implements hook_page_top().
 */
function atest_page_top(array &$page_top) {
  file_get_file_references(File::load(1), NULL, EntityStorageInterface::FIELD_LOAD_CURRENT);
}

Proposed resolution

Add a check if the field definition exists before calling ->getType.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

10.1

Component
File module 

Last updated 3 days ago

Created by

🇸🇮Slovenia alecsmrekar

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024