You are here

public function MenuLinkContentNormalizer::denormalize in Replication 8

Overrides ContentEntityNormalizer::denormalize

File

src/Normalizer/MenuLinkContentNormalizer.php, line 32

Class

MenuLinkContentNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  $denormalized = parent::denormalize($data, $class, $format, $context);
  $entity_type_id = 'menu_link_content';
  $parent = $denormalized instanceof MenuLinkContentInterface ? $denormalized
    ->getParentId() : NULL;
  if (!empty($parent) && strpos($parent, $entity_type_id) === 0) {
    list($type, $uuid, ) = explode(':', $parent);
    if ($type === 'menu_link_content' && $uuid) {
      $storage = $this->entityManager
        ->getStorage($entity_type_id);
      if (!empty($context['workspace'])) {
        $storage
          ->useWorkspace($context['workspace']
          ->id());
      }
      $parent = $storage
        ->loadByProperties([
        'uuid' => $uuid,
      ]);
      $parent = reset($parent);
      if ($parent instanceof MenuLinkContentInterface && $parent
        ->id()) {
        $denormalized->parent->value = $type . ':' . $uuid . ':' . $parent
          ->id();
      }
      elseif (!$parent) {

        // Create a new menu link as stub.
        $selection_instance = $this->selectionManager
          ->getInstance([
          'target_type' => $entity_type_id,
        ]);
        $parent = $selection_instance
          ->createNewEntity($entity_type_id, $entity_type_id, rand(), 1);

        // Indicate that this revision is a stub.
        $parent->_rev->is_stub = TRUE;
        $parent->uuid->value = $uuid;
        $parent->langcode->value = $data['@context']['@language'];
        $parent->link->uri = 'internal:/';
        if (!empty($context['workspace'])) {
          $parent->workspace->entity = $context['workspace'];
        }
        $parent->menu_name->value = $denormalized
          ->getMenuName();
        $parent
          ->save();
        $denormalized->parent->value = $type . ':' . $uuid . ':' . $parent
          ->id();
      }
      $storage
        ->useWorkspace(NULL);
    }
  }
  return $denormalized;
}