You are here

public function ContentEntityNormalizer::denormalize in Replication 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/ContentEntityNormalizer.php \Drupal\replication\Normalizer\ContentEntityNormalizer::denormalize()
4 calls to ContentEntityNormalizer::denormalize()
CropNormalizer::denormalize in src/Normalizer/CropNormalizer.php
Denormalizes data back into an object of the given class.
FileEntityNormalizer::denormalize in src/Normalizer/FileEntityNormalizer.php
Denormalizes data back into an object of the given class.
MenuLinkContentNormalizer::denormalize in src/Normalizer/MenuLinkContentNormalizer.php
Denormalizes data back into an object of the given class.
ParagraphNormalizer::denormalize in src/Normalizer/ParagraphNormalizer.php
Denormalizes data back into an object of the given class.
5 methods override ContentEntityNormalizer::denormalize()
AttachmentNormalizer::denormalize in src/Normalizer/AttachmentNormalizer.php
Denormalizes data back into an object of the given class.
CropNormalizer::denormalize in src/Normalizer/CropNormalizer.php
Denormalizes data back into an object of the given class.
FileEntityNormalizer::denormalize in src/Normalizer/FileEntityNormalizer.php
Denormalizes data back into an object of the given class.
MenuLinkContentNormalizer::denormalize in src/Normalizer/MenuLinkContentNormalizer.php
Denormalizes data back into an object of the given class.
ParagraphNormalizer::denormalize in src/Normalizer/ParagraphNormalizer.php
Denormalizes data back into an object of the given class.

File

src/Normalizer/ContentEntityNormalizer.php, line 186

Class

ContentEntityNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {

  // Make sure these values start as NULL
  $entity_type_id = NULL;
  $entity_uuid = NULL;
  $entity_id = NULL;

  // Get the default language of the entity
  $default_langcode = $data['@context']['@language'];

  // Get all of the configured languages of the site
  $site_languages = $this->languageManager
    ->getLanguages();

  // Resolve the UUID.
  if (empty($entity_uuid) && !empty($data['_id'])) {
    $entity_uuid = $data['_id'];
  }
  else {
    throw new UnexpectedValueException('The uuid value is missing.');
  }

  // Resolve the entity type ID.
  if (isset($data['@type'])) {
    $entity_type_id = $data['@type'];
  }
  elseif (!empty($context['entity_type'])) {
    $entity_type_id = $context['entity_type'];
  }

  // Map data from the UUID index.
  // @todo: {@link https://www.drupal.org/node/2599938 Needs test.}
  if (!empty($entity_uuid)) {
    $uuid_index = isset($context['workspace']) && $context['workspace'] instanceof WorkspaceInterface ? $this->indexFactory
      ->get('multiversion.entity_index.uuid', $context['workspace']) : $this->indexFactory
      ->get('multiversion.entity_index.uuid');
    if ($record = $uuid_index
      ->get($entity_uuid)) {
      $entity_id = $record['entity_id'];
      if (empty($entity_type_id)) {
        $entity_type_id = $record['entity_type_id'];
      }
      elseif ($entity_type_id != $record['entity_type_id']) {
        throw new UnexpectedValueException('The entity_type value does not match the existing UUID record.');
      }
    }
  }
  if (empty($entity_type_id)) {
    throw new UnexpectedValueException('The entity_type value is missing.');
  }

  // Add the _rev field to the $data array.
  $rev = null;
  if (isset($data['_rev'])) {
    $rev = $data['_rev'];
  }
  $revisions = [];
  if (isset($data['_revisions']['start']) && isset($data['_revisions']['ids'])) {
    $revisions = $data['_revisions'];
  }
  $entity_type = $this->entityManager
    ->getDefinition($entity_type_id);
  $id_key = $entity_type
    ->getKey('id');
  $revision_key = $entity_type
    ->getKey('revision');
  $bundle_key = $entity_type
    ->getKey('bundle');
  $translations = [];
  foreach ($data as $key => $translation) {

    // Skip any keys that start with '_' or '@'.
    if (in_array($key[0], [
      '_',
      '@',
    ])) {
      continue;
    }
    elseif (isset($site_languages[$key]) || $key === LanguageInterface::LANGCODE_NOT_SPECIFIED || $key === LanguageInterface::LANGCODE_NOT_APPLICABLE) {
      $context['language'] = $key;
      $translations[$key] = $this
        ->denormalizeTranslation($translation, $entity_id, $entity_uuid, $entity_type_id, $bundle_key, $entity_type, $id_key, $context, $rev, $revisions);
    }
    elseif (is_array($translation) && $this->moduleHandler
      ->moduleExists('language')) {
      $language = ConfigurableLanguage::createFromLangcode($key);
      $language
        ->save();
      $context['language'] = $key;
      $translations[$key] = $this
        ->denormalizeTranslation($translation, $entity_id, $entity_uuid, $entity_type_id, $bundle_key, $entity_type, $id_key, $context, $rev, $revisions);
    }
  }

  // @todo {@link https://www.drupal.org/node/2599926 Use the passed $class to instantiate the entity.}
  $entity = NULL;
  if ($entity_id && $entity_type_id != 'file' && !empty($translations[$default_langcode])) {
    $entity = $this
      ->createEntityInstance($translations[$default_langcode], $entity_type, $format, $context);
  }
  elseif ($entity_type_id == 'file' && !empty($translations[$default_langcode])) {
    unset($translations[$default_langcode][$id_key], $translations[$default_langcode][$revision_key]);
    $translations[$default_langcode]['status'][0]['value'] = FILE_STATUS_PERMANENT;
    $translations[$default_langcode]['uid'][0]['target_id'] = $this->usersMapping
      ->getUidFromConfig();
    $entity = $this
      ->createEntityInstance($translations[$default_langcode], $entity_type, $format, $context);
  }
  elseif (!empty($translations[$default_langcode])) {
    unset($translations[$default_langcode][$id_key], $translations[$default_langcode][$revision_key]);
    $entity = $this
      ->createEntityInstance($translations[$default_langcode], $entity_type, $format, $context);
  }
  if ($entity instanceof ContentEntityInterface) {
    foreach ($site_languages as $site_language) {
      $langcode = $site_language
        ->getId();
      if ($entity
        ->language()
        ->getId() != $langcode && isset($translations[$langcode]) && !$entity
        ->hasTranslation($langcode)) {
        $entity
          ->addTranslation($langcode, $translations[$langcode]);
      }
    }
  }
  if ($entity_id && $entity) {
    $entity
      ->enforceIsNew(FALSE);
    $entity
      ->setNewRevision(FALSE);
    $entity->_rev->is_stub = FALSE;
  }
  Cache::invalidateTags([
    $entity_type_id . '_list',
  ]);
  return $entity;
}