You are here

public function CropNormalizer::denormalize in Replication 8

Overrides ContentEntityNormalizer::denormalize

File

src/Normalizer/CropNormalizer.php, line 37

Class

CropNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  foreach ($data as $key => &$translation) {
    if (in_array($key[0], [
      '_',
      '@',
    ])) {
      continue;
    }
    if (!empty($translation['entity_id'][0]['crop_target_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 ($target_entity_info = $uuid_index
        ->get($translation['entity_id'][0]['crop_target_uuid'])) {
        $translation['entity_id'][0]['value'] = $target_entity_info['entity_id'];
      }
      else {

        // Create a stub for entity referenced by 'entity_id' field.
        $options['target_type'] = $translation['entity_id'][0]['crop_target_entity_type_id'];
        $selection_instance = $this->selectionManager
          ->getInstance($options);
        $target_entity = $selection_instance
          ->createNewEntity($options['target_type'], $translation['entity_id'][0]['crop_target_bundle'], rand(), 1);
        $target_entity->uri->value = "uri:stub";

        // Set the target workspace if we have it in context.
        if (isset($context['workspace']) && $context['workspace'] instanceof WorkspaceInterface && $target_entity
          ->getEntityType()
          ->get('workspace') !== FALSE) {
          $target_entity->workspace->target_id = $context['workspace']
            ->id();
        }
        $target_entity->uuid->value = $translation['entity_id'][0]['crop_target_uuid'];
        $target_entity->_rev->is_stub = TRUE;
        $target_entity
          ->save();
        $translation['entity_id'][0]['value'] = $target_entity
          ->id();
      }
    }
  }
  return parent::denormalize($data, $class, $format);
}