public function ContentEntityNormalizer::denormalize in Replication 8.2
Same name and namespace in other branches
- 8 src/Normalizer/ContentEntityNormalizer.php \Drupal\replication\Normalizer\ContentEntityNormalizer::denormalize()
1 call to ContentEntityNormalizer::denormalize()
- FileEntityNormalizer::denormalize in src/
Normalizer/ FileEntityNormalizer.php - Denormalizes data back into an object of the given class.
2 methods override ContentEntityNormalizer::denormalize()
- AttachmentNormalizer::denormalize in src/
Normalizer/ AttachmentNormalizer.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.
File
- src/
Normalizer/ ContentEntityNormalizer.php, line 178
Class
Namespace
Drupal\replication\NormalizerCode
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) {
$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();
$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/2599946 Move the below update
// logic to the resource plugin instead.}
$storage = $this->entityManager
->getStorage($entity_type_id);
// @todo {@link https://www.drupal.org/node/2599926 Use the passed $class to instantiate the entity.}
if ($entity_id) {
if ($entity = $storage
->load($entity_id) ?: $storage
->loadDeleted($entity_id)) {
if (!empty($translations[$entity
->language()
->getId()])) {
$translation = $translations[$entity
->language()
->getId()];
unset($translation['default_langcode']);
if ($entity_type
->hasKey('bundle')) {
$this
->extractBundleData($translation, $entity_type);
}
$this
->denormalizeFieldData($translation, $entity, $format, $context);
}
}
elseif (isset($translations[$default_langcode][$id_key])) {
unset($translations[$default_langcode][$id_key], $translations[$default_langcode][$revision_key]);
$entity_id = NULL;
$entity = $this
->createEntityInstance($translations[$default_langcode], $entity_type, $format, $context);
}
}
else {
$entity = NULL;
if (!empty($bundle_key) && !empty($translations[$default_langcode][$bundle_key])) {
unset($translations[$default_langcode][$id_key], $translations[$default_langcode][$revision_key]);
$entity = $this
->createEntityInstance($translations[$default_langcode], $entity_type, $format, $context);
}
elseif ($entity_type_id === 'file' && !empty($translations[$default_langcode])) {
if (isset($translations[$default_langcode][$id_key])) {
unset($translations[$default_langcode][$id_key]);
}
if (isset($translations[$default_langcode][$revision_key])) {
unset($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);
}
}
foreach ($site_languages as $site_language) {
$langcode = $site_language
->getId();
if ($entity
->language()
->getId() != $langcode && isset($translations[$langcode])) {
$entity
->addTranslation($langcode, $translations[$langcode]);
}
}
if ($entity_id) {
$entity
->enforceIsNew(FALSE);
$entity
->setNewRevision(FALSE);
$entity->_rev->is_stub = FALSE;
}
Cache::invalidateTags([
$entity_type_id . '_list',
]);
return $entity;
}