TaxonomyTermProcessor.php in Content Synchronizer 8
File
src/Plugin/content_synchronizer/entity_processor/TaxonomyTermProcessor.php
View source
<?php
namespace Drupal\content_synchronizer\Plugin\content_synchronizer\entity_processor;
use Drupal\content_synchronizer\Entity\ImportEntity;
use Drupal\content_synchronizer\Events\ImportEvent;
use Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase;
use Drupal\content_synchronizer\Processors\ExportEntityWriter;
use Drupal\content_synchronizer\Processors\ImportProcessor;
use Drupal\Core\Entity\Entity;
use Drupal\taxonomy\Entity\Term;
class TaxonomyTermProcessor extends EntityProcessorBase {
protected static $dependenciesBuffer = [];
protected static $treeBuffer = [];
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 getEntityToImport(array $data, Entity $existingEntity = NULL) {
$currentGid = $data[ExportEntityWriter::FIELD_GID];
$defaultLanguageData = $this
->getDefaultLanguageData($data, FALSE);
if (array_key_exists('parents', $defaultLanguageData)) {
$importProcessor = ImportProcessor::getCurrentImportProcessor();
$import = $importProcessor
->getImport();
foreach ($defaultLanguageData['parents'] as $parentGid) {
if ($import
->gidIsCurrentlyImporting($parentGid)) {
$this
->addParentDependencie($data, $parentGid);
}
elseif ($import
->gidHasAlreadyBeenImported($parentGid)) {
static::$treeBuffer[$currentGid][] = $this
->getGlobalReferenceManager()
->getEntityByGid($parentGid)
->id();
}
else {
$plugin = $this
->getEntityProcessorManager()
->getInstanceByEntityType($this
->getGlobalReferenceManager()
->getEntityTypeFromGid($parentGid));
if ($entityData = $import
->getEntityDataFromGid($parentGid)) {
$parent = $plugin
->import($entityData);
static::$treeBuffer[$currentGid][] = $parent
->id();
}
}
}
}
return parent::getEntityToImport($data, $existingEntity);
}
protected function addParentDependencie(array $data, $parentGid) {
static::$dependenciesBuffer[$parentGid][] = $data;
}
public function onImportedEntity(ImportEvent $event) {
$gid = $event
->getGid();
$entity = $event
->getEntity();
if (array_key_exists($gid, static::$dependenciesBuffer)) {
foreach (static::$dependenciesBuffer[$gid] as $childData) {
$child = $this
->getGlobalReferenceManager()
->getEntityByGid($childData[ExportEntityWriter::FIELD_GID]);
$alreadyAddedParents = $this
->getParentsTerms($child);
$alreadyAddedParents[] = $entity
->id();
$child
->set('parent', $alreadyAddedParents);
$child
->save();
}
}
if (array_key_exists($gid, static::$treeBuffer)) {
$entity
->set('parent', static::$treeBuffer[$gid]);
$entity
->save();
}
}
public function getDataToExport(Entity $entityToExport) {
$data = parent::getDataToExport($entityToExport);
$data['parents'] = [];
$parents = $this
->getParentsTerms($entityToExport);
if (!empty($parents)) {
$plugin = $this
->getEntityProcessorManager()
->getInstanceByEntityType($entityToExport
->getEntityTypeId());
foreach ($parents as $parent) {
if ($parentGid = $plugin
->export($parent)) {
$data['parents'][] = $parentGid;
}
}
}
return $data;
}
protected function getParentsTerms(Term $child) {
return \Drupal::entityTypeManager()
->getStorage($child
->getEntityTypeId())
->loadParents($child
->id());
}
}