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

Created on 9 December 2020, over 4 years ago
Updated 12 February 2024, over 1 year 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 1 day 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.

  • 🇮🇳India nikhil_110

    Patch for 10.x

  • First commit to issue fork.
  • Status changed to Needs review over 2 years ago
  • 🇮🇳India prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan
  • Status changed to Needs work over 2 years ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • last update almost 2 years ago
    29,398 pass
  • First commit to issue fork.
  • 🇩🇪Germany Grevil

    The patch works great!

    Although one small thing, I think we should adjust the table headers to conform with the fileUsage "add()" API documentation:

    * @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.

    So instead of this:

    We should use the headers "Object ID" and "Object type".

  • Status changed to Needs review over 1 year ago
  • 🇩🇪Germany Anybody Porta Westfalica

    Agree with #22, as "Entity type" would be wrong then after this fix.
    Back to needs review!

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Left some small comments on MR,

    Also new fields believe could use a change record to announce it.

  • Pipeline finished with Success
    7 months ago
    Total: 452s
    #324187
  • Pipeline finished with Success
    3 months ago
    Total: 334s
    #426798
  • Pipeline finished with Success
    2 months ago
    Total: 1011s
    #444713
  • Pipeline finished with Success
    2 months ago
    Total: 3798s
    #446627
Production build 0.71.5 2024