function EditorHooks::recordFileUsage

Records file usage of files referenced by formatted text fields.

Each referenced file that is temporally saved will be resaved as permanent.

Parameters

array $uuids: An array of file entity UUIDs.

\Drupal\Core\Entity\EntityInterface $entity: An entity whose fields to inspect for file references.

2 calls to EditorHooks::recordFileUsage()
EditorHooks::entityInsert in core/modules/editor/src/Hook/EditorHooks.php
Implements hook_entity_insert().
EditorHooks::entityUpdate in core/modules/editor/src/Hook/EditorHooks.php
Implements hook_entity_update().

File

core/modules/editor/src/Hook/EditorHooks.php, line 365

Class

EditorHooks
Hook implementations for editor.

Namespace

Drupal\editor\Hook

Code

protected function recordFileUsage(array $uuids, EntityInterface $entity) : void {
  if (!\Drupal::getContainer()->has('file.usage')) {
    return;
  }
  $entity_repository = \Drupal::service('entity.repository');
  $file_usage = \Drupal::service('file.usage');
  foreach ($uuids as $uuid) {
    if ($file = $entity_repository->loadEntityByUuid('file', $uuid)) {
      assert($file instanceof FileInterface);
      if ($file->isTemporary()) {
        $file->setPermanent();
        $file->save();
      }
      $file_usage->add($file, 'editor', $entity->getEntityTypeId(), $entity->id());
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.