You are here

public function ParagraphNormalizer::normalize in Replication 8

Overrides ContentEntityNormalizer::normalize

File

src/Normalizer/ParagraphNormalizer.php, line 18

Class

ParagraphNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function normalize($entity, $format = NULL, array $context = []) {
  $normalized = parent::normalize($entity, $format, $context);
  $langcode = $entity
    ->language()
    ->getId();
  $parent = $entity
    ->getParentEntity();
  if ($parent instanceof ContentEntityInterface && !empty($normalized[$langcode]['parent_id'][0]['value'])) {
    $parent_id_field_info = [
      'entity_type_id' => $parent
        ->getEntityTypeId(),
      'target_uuid' => $parent
        ->uuid(),
    ];
    $bundle_key = $parent
      ->getEntityType()
      ->getKey('bundle');
    $bundle = $parent
      ->bundle();
    if ($bundle_key && $bundle) {
      $parent_id_field_info[$bundle_key] = $bundle;
    }
    $normalized[$langcode]['parent_id'][0]['value'] = $parent_id_field_info;
  }
  return $normalized;
}