You are here

public function FileEntityNormalizer::normalize in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Normalizer/FileEntityNormalizer.php \Drupal\content_sync\Normalizer\FileEntityNormalizer::normalize()

Overrides ContentEntityNormalizer::normalize

File

src/Normalizer/FileEntityNormalizer.php, line 128

Class

FileEntityNormalizer
Adds the file URI to embedded file entities.

Namespace

Drupal\content_sync\Normalizer

Code

public function normalize($object, $format = NULL, array $serializer_context = array()) {
  $data = parent::normalize($object, $format, $serializer_context);

  // The image will be saved in the export directory.
  if (!empty($serializer_context['content_sync_directory_files'])) {
    $uri = $object
      ->getFileUri();
    $scheme = \Drupal::service('stream_wrapper_manager')
      ->getScheme($uri);
    $destination = "{$serializer_context['content_sync_directory_files']}/{$scheme}/";
    $destination = str_replace($scheme . '://', $destination, $uri);
    $this->fileSystem
      ->prepareDirectory($this->fileSystem
      ->dirname($destination), FileSystemInterface::CREATE_DIRECTORY);
    $this->fileSystem
      ->copy($uri, $destination, FileSystemInterface::EXISTS_REPLACE);
  }

  // Set base64-encoded file contents to the "data" property.
  if (!empty($serializer_context['content_sync_file_base_64'])) {
    $file_data = base64_encode(file_get_contents($object
      ->getFileUri()));
    $data['data'] = [
      [
        'value' => $file_data,
      ],
    ];
  }
  return $data;
}