public function LinkItemNormalizer::denormalize in Replication 8.2
Same name and namespace in other branches
- 8 src/Normalizer/LinkItemNormalizer.php \Drupal\replication\Normalizer\LinkItemNormalizer::denormalize()
Overrides FieldItemNormalizer::denormalize
File
- src/
Normalizer/ LinkItemNormalizer.php, line 96
Class
Namespace
Drupal\replication\NormalizerCode
public function denormalize($data, $class, $format = NULL, array $context = []) {
if (isset($data['uri'])) {
$scheme = parse_url($data['uri'], PHP_URL_SCHEME);
if ($scheme != 'internal' && $scheme != 'entity') {
return parent::denormalize($data, $class, $format, $context);
}
$path = parse_url($data['uri'], PHP_URL_PATH);
$path_arguments = explode('/', $path);
if (isset($path[0]) && $path[0] == '/' && isset($path_arguments[1]) && isset($path_arguments[2]) && empty($path_arguments[3])) {
$entity_type = $path_arguments[1];
$entity_uuid = $path_arguments[2];
}
elseif (isset($path[0]) && $path[0] != '/' && isset($path_arguments[0]) && isset($path_arguments[1]) && empty($path_arguments[2])) {
$entity_type = $path_arguments[0];
$entity_uuid = $path_arguments[1];
}
else {
return parent::denormalize($data, $class, $format, $context);
}
$entity_types = array_keys($this->entityTypeManager
->getDefinitions());
if (!in_array($entity_type, $entity_types)) {
return parent::denormalize($data, $class, $format, $context);
}
$entities = $this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $entity_uuid,
]);
$entity = reset($entities);
if (!$entity instanceof EntityInterface) {
$bundle_key = $this->entityTypeManager
->getStorage($entity_type)
->getEntityType()
->getKey('bundle');
if (isset($data[$bundle_key])) {
/** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface $selection_instance */
$selection_instance = $this->selectionManager
->getInstance([
'target_type' => $entity_type,
]);
// We use a temporary label and entity owner ID as this will be
// backfilled later anyhow, when the real entity comes around.
$entity = $selection_instance
->createNewEntity($entity_type, $data[$bundle_key], rand(), 1);
// Set the target workspace if we have it in context.
if (isset($context['workspace']) && $context['workspace'] instanceof WorkspaceInterface && $entity
->getEntityType()
->get('workspace') !== FALSE) {
$entity->workspace->target_id = $context['workspace']
->id();
}
// Set the UUID to what we received to ensure it gets updated when
// the full entity comes around later.
$entity->uuid->value = $entity_uuid;
// Indicate that this revision is a stub.
$entity->_rev->is_stub = TRUE;
$entity
->save();
}
}
if ($entity instanceof EntityInterface) {
$data['uri'] = $scheme == 'entity' ? "{$scheme}:{$entity_type}/" . $entity
->id() : "{$scheme}:/{$entity_type}/" . $entity
->id();
}
}
return parent::denormalize($data, $class, $format, $context);
}