You are here

public function ContentImporter::importEntity in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Importer/ContentImporter.php \Drupal\content_sync\Importer\ContentImporter::importEntity()

Parameters

$decoded_entity:

$entity_type_id:

array $context:

Return value

\Drupal\Core\Entity\ContentEntityInterface

Overrides ContentImporterInterface::importEntity

File

src/Importer/ContentImporter.php, line 38

Class

ContentImporter

Namespace

Drupal\content_sync\Importer

Code

public function importEntity($decoded_entity, $context = []) {
  $context = $this->context + $context;
  if (!empty($context['entity_type'])) {
    $entity_type_id = $context['entity_type'];
  }
  elseif (!empty($decoded_entity['_content_sync']['entity_type'])) {
    $entity_type_id = $decoded_entity['_content_sync']['entity_type'];
  }
  else {
    return NULL;
  }

  // Replace a menu link to a node with an actual one.
  if ($entity_type_id == 'menu_link_content' && !empty($decoded_entity["_content_sync"]["menu_entity_link"])) {
    $decoded_entity = $this
      ->alterMenuLink($decoded_entity);
  }
  $entity_type = $this->entityTypeManager
    ->getDefinition($entity_type_id);

  //Exception for parent null -- allowing the term to be displayed on the taxonomy list.
  if ($entity_type_id == 'taxonomy_term') {
    if (empty($decoded_entity['parent'])) {
      $decoded_entity['parent']['target_id'] = 0;
    }
  }

  //Get Translations before denormalize
  if (!empty($decoded_entity['_translations'])) {
    $entity_translations = $decoded_entity['_translations'];
  }
  $entity = $this->serializer
    ->denormalize($decoded_entity, $entity_type
    ->getClass(), $this->format, $context);
  if (!empty($entity)) {

    // Prevent Anonymous User from being saved.
    if ($entity_type_id == 'user' && !$entity
      ->isNew() && (int) $entity
      ->id() === 0) {
      return $entity;
    }
    $entity = $this
      ->syncEntity($entity);
  }

  // Include Translations
  if ($entity) {
    if (isset($entity_translations) && is_array($entity_translations)) {
      $this
        ->updateTranslation($entity, $entity_type, $entity_translations, $context);
    }
  }
  return $entity;
}