Managed file usage service

Now that we have an idea of the available entity CRUD hooks, we can implement three of them to handle our managed file problem. Because if you remember, managed files are actually represented by the File entity type, so the Entity CRUD hooks get fired for these as well.

To mark a file as being used by something, we can use the DatabaseFileUsageBackend service (file.usage), which is an implementation of the FileUsageInterface. This has a few handy methods that allow us to add a usage or delete it. That is actually what we are going to do next.

What we want to do first is add a file usage whenever a new Importer entity gets created (and a file uploaded with it):

/** * Implements hook_ENTITY_TYPE_insert() for the Importer ...

Get Drupal 8 Module Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.