EntityReferenceFieldItemListProcessor.php in Content Synchronizer 8
File
src/Plugin/content_synchronizer/type_processor/EntityReferenceFieldItemListProcessor.php
View source
<?php
namespace Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor;
use Drupal\content_synchronizer\Events\ImportEvent;
use Drupal\content_synchronizer\Processors\Entity\EntityProcessorPluginManager;
use Drupal\content_synchronizer\Processors\ImportProcessor;
use Drupal\content_synchronizer\Service\GlobalReferenceManager;
use Drupal\Core\Entity\Entity;
use Drupal\Core\TypedData\TypedData;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\content_synchronizer\Processors\Type\TypeProcessorBase;
class EntityReferenceFieldItemListProcessor extends TypeProcessorBase {
protected static $dependenciesBuffer = [];
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$dispatcher = \Drupal::service('event_dispatcher');
$dispatcher
->addListener(ImportEvent::ON_ENTITY_IMPORTER, [
$this,
'onImportedEntity',
]);
}
public function getExportedData(TypedData $propertyData) {
$data = [];
$entityProcessorManager = \Drupal::service(EntityProcessorPluginManager::SERVICE_NAME);
$order = 0;
foreach ($propertyData
->referencedEntities() as $entity) {
$plugin = $entityProcessorManager
->getInstanceByEntityType($entity
->getEntityTypeId());
if (get_class($entity) != "Drupal\\user\\Entity\\User") {
if ($gid = $plugin
->export($entity)) {
$data[$order] = $gid;
$order++;
}
}
}
return $data;
}
public function initImportedEntity(Entity $entityToImport, $propertyId, array $data) {
$referenceManager = \Drupal::service(GlobalReferenceManager::SERVICE_NAME);
$importProcessor = ImportProcessor::getCurrentImportProcessor();
$import = $importProcessor
->getImport();
$pluginManager = \Drupal::service(EntityProcessorPluginManager::SERVICE_NAME);
$referenceField = $entityToImport
->get($propertyId);
if (array_key_exists($propertyId, $data) && is_array($data[$propertyId])) {
while ($referenceField
->count() > 0) {
$referenceField
->removeItem(0);
}
foreach ($data[$propertyId] as $order => $entityGid) {
if ($import
->gidIsCurrentlyImporting($entityGid)) {
$referenceField
->appendItem(NULL);
$this
->addDependencie($entityGid, $referenceField, $order);
}
elseif ($import
->gidHasAlreadyBeenImported($entityGid)) {
$referenceField
->appendItem($referenceManager
->getEntityByGid($entityGid));
}
else {
$plugin = $pluginManager
->getInstanceByEntityType($referenceManager
->getEntityTypeFromGid($entityGid));
if ($entityData = $import
->getEntityDataFromGid($entityGid)) {
$referencedEntity = $plugin
->import($entityData);
$referenceField
->appendItem($referencedEntity);
}
}
}
}
}
public function addDependencie($gid, EntityReferenceFieldItemList $field, $order) {
self::$dependenciesBuffer[$gid][] = [
'field' => $field,
'order' => $order,
];
}
public function onImportedEntity(ImportEvent $event) {
$gid = $event
->getGid();
$entity = $event
->getEntity();
if (array_key_exists($gid, self::$dependenciesBuffer)) {
foreach (self::$dependenciesBuffer[$gid] as $parent) {
$parent['field'][$parent['order']] = $entity;
}
}
}
}