You are here

public function FileEntityNormalizer::normalize in Better Normalizers 8

Overrides ContentEntityNormalizer::normalize

File

src/Normalizer/FileEntityNormalizer.php, line 41

Class

FileEntityNormalizer
Normalizer for File entity.

Namespace

Drupal\better_normalizers\Normalizer

Code

public function normalize($entity, $format = NULL, array $context = array()) {
  $data = parent::normalize($entity, $format, $context);
  if (!isset($context['included_fields']) || in_array('data', $context['included_fields'])) {

    // Save base64-encoded file contents to the "data" property.
    $file_data = base64_encode(file_get_contents($entity
      ->getFileUri()));
    $data += array(
      'data' => array(
        array(
          'value' => $file_data,
        ),
      ),
    );
  }
  return $data;
}