function DefaultContentSubscriber::preExport

Reacts before an entity is exported.

Parameters

\Drupal\Core\DefaultContent\PreExportEvent $event: The event object.

File

core/modules/file/src/EventSubscriber/DefaultContentSubscriber.php, line 35

Class

DefaultContentSubscriber
Subscribes to default content-related events.

Namespace

Drupal\file\EventSubscriber

Code

public function preExport(PreExportEvent $event) : void {
  $entity = $event->entity;
  if ($entity instanceof FileInterface) {
    $uri = $entity->getFileUri();
    // Ensure the file has a name (`getFilename()` may return NULL).
    $name = $entity->getFilename() ?? basename($uri);
    $entity->setFilename($name);
    if (file_exists($uri)) {
      $event->metadata
        ->addAttachment($uri, $name);
    }
    else {
      $this->logger?->warning('The file (%uri) associated with file entity %name does not exist.', [
        '%uri' => $uri,
        '%name' => $entity->label(),
      ]);
    }
  }
}

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