You are here

public function FileItemNormalizer::normalize in Better Normalizers 8

Overrides EntityReferenceItemNormalizer::normalize

File

src/Normalizer/FileItemNormalizer.php, line 34

Class

FileItemNormalizer
Converts File items, including display and description values.

Namespace

Drupal\better_normalizers\Normalizer

Code

public function normalize($field_item, $format = NULL, array $context = array()) {

  /** @var \Drupal\file\Plugin\Field\FieldType\FileItem $field_item */
  $data = parent::normalize($field_item, $format, $context);

  // Copied from parent implementation.
  $field_name = $field_item
    ->getParent()
    ->getName();
  $entity = $field_item
    ->getEntity();
  $field_uri = $this->linkManager
    ->getRelationUri($entity
    ->getEntityTypeId(), $entity
    ->bundle(), $field_name);

  // Add any field-specific data.
  $data['_embedded'][$field_uri][0] += $field_item
    ->getValue();
  unset($data['_embedded'][$field_uri][0]['target_id']);
  return $data;
}