You are here

public function EntityProcessorBase::getEntityToImport in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Processors/Entity/EntityProcessorBase.php \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase::getEntityToImport()
  2. 3.x src/Processors/Entity/EntityProcessorBase.php \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase::getEntityToImport()

Return the entity to import.

Parameters

array $data: The data to import.

\Drupal\Core\Entity\Entity $entityToImport: The existing entity to update.

Overrides EntityProcessorInterface::getEntityToImport

5 calls to EntityProcessorBase::getEntityToImport()
EntityProcessorBase::import in src/Processors/Entity/EntityProcessorBase.php
Create or update entity with data :.
FileProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/FileProcessor.php
Return the entity to import.
NodeProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/NodeProcessor.php
Return the entity to import.
ParagraphProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/ParagraphProcessor.php
Return the entity to import.
TaxonomyTermProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/TaxonomyTermProcessor.php
Return the entity to import.
4 methods override EntityProcessorBase::getEntityToImport()
FileProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/FileProcessor.php
Return the entity to import.
NodeProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/NodeProcessor.php
Return the entity to import.
ParagraphProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/ParagraphProcessor.php
Return the entity to import.
TaxonomyTermProcessor::getEntityToImport in src/Plugin/content_synchronizer/entity_processor/TaxonomyTermProcessor.php
Return the entity to import.

File

src/Processors/Entity/EntityProcessorBase.php, line 410

Class

EntityProcessorBase
The entity processor base.

Namespace

Drupal\content_synchronizer\Processors\Entity

Code

public function getEntityToImport(array $data, Entity $entityToImport = NULL) {
  if ($entityToImport) {
    $backup = clone $entityToImport;
  }

  // Create Entity.
  if (is_null($entityToImport)) {
    try {
      $typeId = $this
        ->getGlobalReferenceManager()
        ->getEntityTypeFromGid($data[ExportEntityWriter::FIELD_GID]);
      $defaultData = $this
        ->getDefaultLanguageData($data);

      // Get type manager.

      /** @var \Drupal\Core\Entity\ContentEntityType $typeManager */
      $typeManager = \Drupal::entityTypeManager()
        ->getDefinition($typeId);
      $bundleKey = $typeManager
        ->getKey('bundle');

      /** @var \Drupal\Core\Entity\EntityFieldManager $entityFieldManager */
      $entityFieldManager = \Drupal::service('entity_field.manager');
      $baseDefinitions = $entityFieldManager
        ->getFieldDefinitions($typeId, $data[$bundleKey]);
      $createData = array_intersect_key($defaultData, $baseDefinitions);
      $entityToImport = \Drupal::entityTypeManager()
        ->getStorage($typeId)
        ->create($createData);
    } catch (\Exception $e) {
      \Drupal::messenger()
        ->addError('Import Process : ' . $e
        ->getMessage() . ' in "' . __METHOD__ . '()"');
      return NULL;
    }
  }

  // Properties not to import.
  $propertyIdsNotToImport = $this
    ->getPropertiesIdsNotToExportList();

  // Get the existing translations.
  $alreadyExistingEntityTranslations = $this
    ->getEntityTranslations($entityToImport);

  // Update data for each translation.
  foreach ($data[self::KEY_TRANSLATIONS] as $languageId => $translationData) {
    if (!array_key_exists($languageId, $alreadyExistingEntityTranslations)) {
      if ($translation = $this
        ->createNewTranslation($languageId, $entityToImport, $translationData)) {
        $alreadyExistingEntityTranslations[$languageId] = $translation;
      }
      else {
        continue;
      }
    }
    $entityToUpdate = $alreadyExistingEntityTranslations[$languageId];

    // Parse each property of the entity.
    foreach ($entityToUpdate
      ->getTypedData()
      ->getProperties() as $propertyId => $propertyData) {

      // Check properties to import :
      if (!in_array($propertyId, $propertyIdsNotToImport)) {

        /** @var \Drupal\content_synchronizer\Processors\Type\TypeProcessorBase $plugin */
        if ($plugin = $this
          ->getTypeProcessorManager()
          ->getInstanceByFieldType(get_class($propertyData))) {
          $plugin
            ->initImportedEntity($entityToUpdate, $propertyId, $translationData);
        }
      }
    }

    // Save translation.
    if ($entityToImport
      ->language()
      ->getId() != $entityToUpdate
      ->language()
      ->getId()) {
      $this
        ->setChangedTime($entityToImport, $translationData);
      $this
        ->getEntityPublisher()
        ->saveEntity($entityToUpdate, NULL, $backup, $translationData);
    }
  }
  return $entityToImport;
}