You are here

public function LinkItemNormalizer::denormalize in Replication 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/LinkItemNormalizer.php \Drupal\replication\Normalizer\LinkItemNormalizer::denormalize()

Overrides FieldItemNormalizer::denormalize

File

src/Normalizer/LinkItemNormalizer.php, line 116

Class

LinkItemNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  if (isset($data['uri'])) {
    $scheme = parse_url($data['uri'], PHP_URL_SCHEME);
    if (!in_array($scheme, [
      'internal',
      'entity',
    ]) || !isset($data['_entity_uuid']) || !isset($data['_entity_type'])) {
      return parent::denormalize($data, $class, $format, $context);
    }
    $entity_uuid = $data['_entity_uuid'];
    $entity_type = $data['_entity_type'];
    $entity = NULL;
    if (isset($context['workspace']) && $context['workspace'] instanceof WorkspaceInterface) {
      $entities = $this->entityTypeManager
        ->getStorage($entity_type)
        ->useWorkspace($context['workspace']
        ->id())
        ->loadByProperties([
        'uuid' => $entity_uuid,
      ]);
      $entity = reset($entities);
    }
    if (!$entity instanceof ContentEntityInterface) {
      $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'] = str_replace($entity_uuid, $entity
        ->id(), $data['uri']);
      unset($data['_entity_uuid']);
      unset($data['_entity_type']);
      $bundle_key = $entity
        ->getEntityType()
        ->getKey('bundle');
      $bundle = $entity
        ->bundle();
      if ($bundle_key && $bundle) {
        unset($data[$bundle_key]);
      }
    }
  }
  return parent::denormalize($data, $class, $format, $context);
}