You are here

public function EntityReferenceFieldItemListProcessor::initImportedEntity in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/content_synchronizer/type_processor/EntityReferenceFieldItemListProcessor.php \Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor\EntityReferenceFieldItemListProcessor::initImportedEntity()
  2. 3.x src/Plugin/content_synchronizer/type_processor/EntityReferenceFieldItemListProcessor.php \Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor\EntityReferenceFieldItemListProcessor::initImportedEntity()

Init the $propertyId value in the entity to import.

Parameters

\Drupal\Core\Entity\Entity $entityToImport: The entity to import.

string $propertyId: The property id.

array $data: The data to import.

Overrides TypeProcessorBase::initImportedEntity

File

src/Plugin/content_synchronizer/type_processor/EntityReferenceFieldItemListProcessor.php, line 73

Class

EntityReferenceFieldItemListProcessor
Plugin implementation For the type processor .

Namespace

Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor

Code

public function initImportedEntity(Entity $entityToImport, $propertyId, array $data) {

  /** @var \Drupal\content_synchronizer\Service\GlobalReferenceManager $referenceManager */
  $referenceManager = \Drupal::service(GlobalReferenceManager::SERVICE_NAME);

  /** @var \Drupal\content_synchronizer\Processors\ImportProcessor $importProcessor */
  $importProcessor = ImportProcessor::getCurrentImportProcessor();

  /** @var \Drupal\content_synchronizer\Entity\ImportEntity $import */
  $import = $importProcessor
    ->getImport();

  /** @var \Drupal\content_synchronizer\Processors\Entity\EntityProcessorPluginManager $pluginManager */
  $pluginManager = \Drupal::service(EntityProcessorPluginManager::SERVICE_NAME);

  /** @var \Drupal\Core\Field\EntityReferenceFieldItemList $referenceField */
  $referenceField = $entityToImport
    ->get($propertyId);

  // Parse list of entities :
  if (array_key_exists($propertyId, $data) && is_array($data[$propertyId])) {

    // Empty previous references.
    while ($referenceField
      ->count() > 0) {
      $referenceField
        ->removeItem(0);
    }
    foreach ($data[$propertyId] as $order => $entityGid) {

      // If the entity to reference is currently importing, then we cannot add it to the reference because it probably do not have an id yet.
      if ($import
        ->gidIsCurrentlyImporting($entityGid)) {
        $referenceField
          ->appendItem(NULL);
        $this
          ->addDependencie($entityGid, $referenceField, $order);
      }
      elseif ($import
        ->gidHasAlreadyBeenImported($entityGid)) {
        $referenceField
          ->appendItem($referenceManager
          ->getEntityByGid($entityGid));
      }
      else {

        // Get the plugin of the entity :

        /** @var \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase $plugin */
        $plugin = $pluginManager
          ->getInstanceByEntityType($referenceManager
          ->getEntityTypeFromGid($entityGid));
        if ($entityData = $import
          ->getEntityDataFromGid($entityGid)) {
          $referencedEntity = $plugin
            ->import($entityData);
          $referenceField
            ->appendItem($referencedEntity);
        }
      }
    }
  }
}