You are here

public function ContentEntityNormalizer::normalize in Replication 8.2

Same name and namespace in other branches
  1. 8 src/Normalizer/ContentEntityNormalizer.php \Drupal\replication\Normalizer\ContentEntityNormalizer::normalize()
3 calls to ContentEntityNormalizer::normalize()
AttachmentNormalizer::normalize in src/Normalizer/AttachmentNormalizer.php
Normalizes an object into a set of arrays/scalars.
FileEntityNormalizer::normalize in src/Normalizer/FileEntityNormalizer.php
Normalizes an object into a set of arrays/scalars.
TaxonomyTermNormalizer::normalize in src/Normalizer/TaxonomyTermNormalizer.php
Normalizes an object into a set of arrays/scalars.
3 methods override ContentEntityNormalizer::normalize()
AttachmentNormalizer::normalize in src/Normalizer/AttachmentNormalizer.php
Normalizes an object into a set of arrays/scalars.
FileEntityNormalizer::normalize in src/Normalizer/FileEntityNormalizer.php
Normalizes an object into a set of arrays/scalars.
TaxonomyTermNormalizer::normalize in src/Normalizer/TaxonomyTermNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

src/Normalizer/ContentEntityNormalizer.php, line 87

Class

ContentEntityNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function normalize($entity, $format = NULL, array $context = []) {
  $workspace = isset($entity->workspace->entity) ? $entity->workspace->entity : null;
  $rev_tree_index = $this->indexFactory
    ->get('multiversion.entity_index.rev.tree', $workspace);
  $entity_type_id = $context['entity_type'] = $entity
    ->getEntityTypeId();
  $entity_type = $this->entityManager
    ->getDefinition($entity_type_id);
  $id_key = $entity_type
    ->getKey('id');
  $revision_key = $entity_type
    ->getKey('revision');
  $uuid_key = $entity_type
    ->getKey('uuid');
  $entity_uuid = $entity
    ->uuid();
  $entity_default_language = $entity
    ->language();
  $entity_languages = $entity
    ->getTranslationLanguages();

  // Create the basic data array with JSON-LD data.
  $data = [
    '@context' => [
      '_id' => '@id',
      '@language' => $entity_default_language
        ->getId(),
    ],
    '@type' => $entity_type_id,
    '_id' => $entity_uuid,
  ];

  // New or mocked entities might not have a rev yet.
  if (!empty($entity->_rev->value)) {
    $data['_rev'] = $entity->_rev->value;
  }

  // Loop through each language of the entity
  $field_definitions = $entity
    ->getFieldDefinitions();
  foreach ($entity_languages as $entity_language) {
    $translation = $entity
      ->getTranslation($entity_language
      ->getId());

    // Add the default language
    $data[$entity_language
      ->getId()] = [
      '@context' => [
        '@language' => $entity_language
          ->getId(),
      ],
    ];
    foreach ($translation as $name => $field) {

      // Add data for each field (through the field's normalizer.
      $field_type = $field_definitions[$name]
        ->getType();
      $items = $this->serializer
        ->normalize($field, $format, $context);
      if ($field_type == 'password') {
        continue;
      }
      $data[$entity_language
        ->getId()][$name] = $items;
    }

    // Override the normalization for the _deleted special field, just so that we
    // follow the API spec.
    if (isset($translation->_deleted->value) && $translation->_deleted->value == TRUE) {
      $data[$entity_language
        ->getId()]['_deleted'] = TRUE;
      $data['_deleted'] = TRUE;
    }
    elseif (isset($data[$entity_language
      ->getId()]['_deleted'])) {
      unset($data[$entity_language
        ->getId()]['_deleted']);
    }
  }

  // @todo: Needs test.}
  // Normalize the $entity->_rev->revisions value.
  if (!empty($entity->_rev->revisions)) {
    $data['_revisions']['ids'] = $entity->_rev->revisions;
    $data['_revisions']['start'] = count($data['_revisions']['ids']);
  }
  if (!empty($context['query']['conflicts'])) {
    $conflicts = $rev_tree_index
      ->getConflicts($entity_uuid);
    foreach ($conflicts as $rev => $status) {
      $data['_conflicts'][] = $rev;
    }
  }

  // Finally we remove certain fields that are "local" to this host.
  unset($data['workspace'], $data[$id_key], $data[$revision_key], $data[$uuid_key]);
  foreach ($entity_languages as $entity_language) {
    $langcode = $entity_language
      ->getId();
    unset($data[$langcode]['workspace'], $data[$langcode][$id_key], $data[$langcode][$revision_key], $data[$langcode][$uuid_key]);
  }
  $event = new ReplicationContentDataAlterEvent($entity, $data, $format, $context);
  $this->dispatcher
    ->dispatch(ReplicationDataEvents::ALTER_CONTENT_DATA, $event);
  return $event
    ->getData();
}