File Usage statistics for Views treat "$type" as entity_type_id but API doesn't require that

Created on 9 December 2020, almost 4 years ago
Updated 12 February 2024, 9 months ago

Problem/Motivation

Drupal Core API provides a file.usage service that provides the ability for developers to track file usage of a specific file in different places and for different purposes.

This service implements \Drupal\file\FileUsage\FileUsageInterface that describe adding file usage as:

  /**
   * Records that a module is using a file.
   *
   * Examples:
   * - A module that associates files with nodes, so $type would be
   *   'node' and $id would be the node's nid. Files for all revisions are
   *   stored within a single nid.
   * - The User module associates an image with a user, so $type would be 'user'
   *   and the $id would be the user's uid.
   *
   * @param \Drupal\file\FileInterface $file
   *   A file entity.
   * @param string $module
   *   The name of the module using the file.
   * @param string $type
   *   The type of the object that contains the referenced file.
   * @param string $id
   *   The unique ID of the object containing the referenced file.
   * @param int $count
   *   (optional) The number of references to add to the object. Defaults to 1.
   */
  public function add(FileInterface $file, $module, $type, $id, $count = 1);

The $type and $id, according to that documentation, can be any string.

For example, I have a form to upload product prices. I track those files by set $type = 'form' and $id = $this->getFormId(). Because this file used by that form, not by specific node or user.

The API (service) allows me to do that and works perfectly, but Views integration doesn't.

In \Drupal\file\FileViewsData::getViewsData where file usage is linked it says "Relate file entities to their usage.". But there is no requirement that usage will be linked on entity type, but views assumes that, and the leads to HTTP 500 error for such usages.

For example above, views trying to load entity type 'form' and end with fatal error:

Drupal\Component\Plugin\Exception\PluginNotFoundException: The "form" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 150 of core/lib/Drupal/Core/Entity/EntityTypeManager.php).

Steps to reproduce

1. Add file usage linked to anything except entities:

$file = File::load(1);
\Drupal::service('file.usage')->add($file, 'example', 'form', 'my_awesome_form');

2. Go to /admin/content/files/usage/1
3. You will see HTTP 500 because Views trying to load entity type 'form'.

The \Drupal\file\FileUsage\FileUsageInterface::listUsage works as expected:

[
  'example' => [
    'form' => [
      'my_awesome_form' => '1',
    ],
  ],
]

Proposed resolution

1. Improve Views integration and remove the requirement for type to be an entity type ID, because this is not required by an API. Show those usages without any links.
2. Change API and description for \Drupal\file\FileUsage\FileUsageInterface::add so it requires $type to be entity_type_id.

I think, in current situation this is Views integration is only the problem, because API works as expected and very useful. Limiting it to the entities only, will make no sense for provided example and other use cases, because that file used by the specific form, not the specific user or entity type.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
File system 

Last updated about 11 hours ago

Created by

🇷🇺Russia niklan Russia, Perm

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

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024