You are here

public function FileEntityNormalizer::normalize in Replication 8

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

Overrides ContentEntityNormalizer::normalize

File

src/Normalizer/FileEntityNormalizer.php, line 51

Class

FileEntityNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function normalize($data, $format = NULL, array $context = []) {
  $normalized = parent::normalize($data, $format, $context);
  $file_system = \Drupal::service('file_system');
  $uri = $data
    ->getFileUri();
  if (empty($uri)) {
    return $normalized;
  }
  if ($file_contents = @file_get_contents($uri)) {
    if (in_array($file_system
      ->uriScheme($uri), [
      'public',
      'private',
    ]) == FALSE) {
      $file_data = '';
    }
    else {
      $file_data = base64_encode($file_contents);
    }

    // @todo {@link https://www.drupal.org/node/2600360 Add revpos and other missing properties to the result array.}
    $normalized['@attachment'] = [
      'uuid' => $data
        ->uuid(),
      'uri' => $uri,
      'content_type' => $data
        ->getMimeType(),
      'digest' => 'md5-' . base64_encode(md5($file_contents)),
      'length' => $data
        ->getSize(),
      'data' => $file_data,
    ];
  }
  return $normalized;
}