class TaxonomyTermProcessor in Content Synchronizer 8
Same name and namespace in other branches
- 8.2 src/Plugin/content_synchronizer/entity_processor/TaxonomyTermProcessor.php \Drupal\content_synchronizer\Plugin\content_synchronizer\entity_processor\TaxonomyTermProcessor
- 3.x src/Plugin/content_synchronizer/entity_processor/TaxonomyTermProcessor.php \Drupal\content_synchronizer\Plugin\content_synchronizer\entity_processor\TaxonomyTermProcessor
Plugin implementation of the 'accordion' formatter.
Plugin annotation
@EntityProcessor(
id = "content_synchronizer_taxonomy_term_processor",
entityType = "taxonomy_term"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase implements EntityProcessorInterface
- class \Drupal\content_synchronizer\Plugin\content_synchronizer\entity_processor\TaxonomyTermProcessor
- class \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase implements EntityProcessorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TaxonomyTermProcessor
File
- src/
Plugin/ content_synchronizer/ entity_processor/ TaxonomyTermProcessor.php, line 21
Namespace
Drupal\content_synchronizer\Plugin\content_synchronizer\entity_processorView source
class TaxonomyTermProcessor extends EntityProcessorBase {
/**
* The dependencies buffer.
*
* @var array
*/
protected static $dependenciesBuffer = [];
/**
* Tree buffer.
*
* @var array
*/
protected static $treeBuffer = [];
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
// Listen import event.
/** @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher $dispatcher */
$dispatcher = \Drupal::service('event_dispatcher');
$dispatcher
->addListener(ImportEvent::ON_ENTITY_IMPORTER, [
$this,
'onImportedEntity',
]);
}
/**
* {@inheritdoc}
*/
public function getEntityToImport(array $data, Entity $existingEntity = NULL) {
// Init tree process.
$currentGid = $data[ExportEntityWriter::FIELD_GID];
$defaultLanguageData = $this
->getDefaultLanguageData($data, FALSE);
if (array_key_exists('parents', $defaultLanguageData)) {
/** @var \Drupal\content_synchronizer\Processors\ImportProcessor $importProcessor */
$importProcessor = ImportProcessor::getCurrentImportProcessor();
/** @var \Drupal\content_synchronizer\Entity\ImportEntity $import */
$import = $importProcessor
->getImport();
foreach ($defaultLanguageData['parents'] as $parentGid) {
// 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($parentGid)) {
$this
->addParentDependencie($data, $parentGid);
}
elseif ($import
->gidHasAlreadyBeenImported($parentGid)) {
static::$treeBuffer[$currentGid][] = $this
->getGlobalReferenceManager()
->getEntityByGid($parentGid)
->id();
}
else {
// Get the plugin of the entity :
/** @var \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase $plugin */
$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);
}
/**
* Add parent dependencie to avoid circular dependencies.
*
* @param array $data
* The child array data.
* @param string $parentGid
* The parent gid.
*/
protected function addParentDependencie(array $data, $parentGid) {
static::$dependenciesBuffer[$parentGid][] = $data;
}
/**
* Action on Entity import end.
*
* @param \Drupal\content_synchronizer\Events\ImportEvent $event
* The event.
*/
public function onImportedEntity(ImportEvent $event) {
$gid = $event
->getGid();
$entity = $event
->getEntity();
// Add parent dependencies.
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();
}
}
// Save data.
if (array_key_exists($gid, static::$treeBuffer)) {
$entity
->set('parent', static::$treeBuffer[$gid]);
$entity
->save();
}
}
/**
* {@inheritdoc}
*/
public function getDataToExport(Entity $entityToExport) {
// Init data to export:
$data = parent::getDataToExport($entityToExport);
// Get parents Terms.
$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;
}
/**
* Return the list of parents terms.
*
* @param \Drupal\taxonomy\Entity\Term $child
* The child term.
*
* @return mixed
* The parents terms.
*/
protected function getParentsTerms(Term $child) {
return \Drupal::entityTypeManager()
->getStorage($child
->getEntityTypeId())
->loadParents($child
->id());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityProcessorBase:: |
protected | property | The entity processor manager service. | |
EntityProcessorBase:: |
protected | property | The entity publisher service. | |
EntityProcessorBase:: |
protected | property | The current entity type. | |
EntityProcessorBase:: |
protected | property | The global reference manager service. | |
EntityProcessorBase:: |
protected | property | ||
EntityProcessorBase:: |
protected | property | The type processor manager service. | |
EntityProcessorBase:: |
private | function | Check if entity's bundle exist, create-it | |
EntityProcessorBase:: |
private | function | Create missing Bundle | |
EntityProcessorBase:: |
protected | function | Return a translation. | |
EntityProcessorBase:: |
final public | function | Export the entity and return the gid if exists, else false. | |
EntityProcessorBase:: |
constant | |||
EntityProcessorBase:: |
public | function | Return the data of the default language of the passed data. | |
EntityProcessorBase:: |
protected | function | Get the global reference entity. | |
EntityProcessorBase:: |
protected | function | Get the EntityProcessor plugin manager. | |
EntityProcessorBase:: |
public | function | Return the entity saver service. | |
EntityProcessorBase:: |
protected | function | Return the entity translations. | |
EntityProcessorBase:: |
public | function | Return the current entity type. | |
EntityProcessorBase:: |
final protected | function | Get the contentSyncManager. | |
EntityProcessorBase:: |
public | function |
Get the array of the property of the entity not to export. Overrides EntityProcessorInterface:: |
|
EntityProcessorBase:: |
protected | function | Get the TypeProcessor plugin manager. | |
EntityProcessorBase:: |
final public | function | Create or update entity with data :. | |
EntityProcessorBase:: |
constant | |||
EntityProcessorBase:: |
constant | |||
EntityProcessorBase:: |
protected | function | Callback when the entity has been imported. | |
EntityProcessorBase:: |
protected | function | Update the changed time form the data array. | |
EntityProcessorBase:: |
public | function | Set the current entity type. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TaxonomyTermProcessor:: |
protected static | property | The dependencies buffer. | |
TaxonomyTermProcessor:: |
protected static | property | Tree buffer. | |
TaxonomyTermProcessor:: |
protected | function | Add parent dependencie to avoid circular dependencies. | |
TaxonomyTermProcessor:: |
public | function |
Return the data to export. Overrides EntityProcessorBase:: |
|
TaxonomyTermProcessor:: |
public | function |
Return the entity to import. Overrides EntityProcessorBase:: |
|
TaxonomyTermProcessor:: |
protected | function | Return the list of parents terms. | |
TaxonomyTermProcessor:: |
public | function | Action on Entity import end. | |
TaxonomyTermProcessor:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |