function DefaultContentSubscriber::preExport

Same name in this branch
  1. 11.x core/modules/media/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\media\EventSubscriber\DefaultContentSubscriber::preExport()
  2. 11.x core/modules/layout_builder/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\layout_builder\EventSubscriber\DefaultContentSubscriber::preExport()
  3. 11.x core/modules/content_moderation/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\content_moderation\EventSubscriber\DefaultContentSubscriber::preExport()
  4. 11.x core/modules/link/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\link\EventSubscriber\DefaultContentSubscriber::preExport()
  5. 11.x core/modules/path/src/EventSubscriber/DefaultContentSubscriber.php \Drupal\path\EventSubscriber\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.