class EntityReferenceFieldItemListProcessor in Content Synchronizer 8.2
Same name and namespace in other branches
- 8 src/Plugin/content_synchronizer/type_processor/EntityReferenceFieldItemListProcessor.php \Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor\EntityReferenceFieldItemListProcessor
- 3.x src/Plugin/content_synchronizer/type_processor/EntityReferenceFieldItemListProcessor.php \Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor\EntityReferenceFieldItemListProcessor
Plugin implementation For the type processor .
Plugin annotation
@TypeProcessor(
id = "content_synchronizer_entity_reference_field_item_list_type_processor",
fieldType = "Drupal\Core\Field\EntityReferenceFieldItemList"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\content_synchronizer\Processors\Type\TypeProcessorBase implements TypeProcessorInterface
- class \Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor\EntityReferenceFieldItemListProcessor
- class \Drupal\content_synchronizer\Processors\Type\TypeProcessorBase implements TypeProcessorInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of EntityReferenceFieldItemListProcessor
File
- src/
Plugin/ content_synchronizer/ type_processor/ EntityReferenceFieldItemListProcessor.php, line 22
Namespace
Drupal\content_synchronizer\Plugin\content_synchronizer\type_processorView source
class EntityReferenceFieldItemListProcessor extends TypeProcessorBase {
protected static $dependenciesBuffer = [];
/**
* {@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',
]);
}
/**
* Return export data array.
*
* @param \Drupal\Core\TypedData\TypedData $propertyData
* The propertyData.
*
* @return array
* export data.
*/
public function getExportedData(TypedData $propertyData) {
$data = [];
// Init processor service.
/** @var \Drupal\content_synchronizer\Processors\Entity\EntityProcessorPluginManager $entityProcessorManager */
$entityProcessorManager = \Drupal::service(EntityProcessorPluginManager::SERVICE_NAME);
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$order = 0;
foreach ($propertyData
->referencedEntities() as $entity) {
/** @var \Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase $plugin */
$plugin = $entityProcessorManager
->getInstanceByEntityType($entity
->getEntityTypeId());
if (get_class($entity) != "Drupal\\user\\Entity\\User") {
if ($gid = $plugin
->export($entity)) {
$data[$order] = $gid;
$order++;
}
}
}
return $data;
}
/**
* {@inheritdoc}
*/
public function initImportedEntity(EntityInterface $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);
}
}
}
}
}
/**
* Add dependencies to importing data.
*
* @param string $gid
* The gid.
* @param \Drupal\Core\Field\EntityReferenceFieldItemList $field
* The field.
* @param int $order
* The order.
*/
public function addDependencie($gid, EntityReferenceFieldItemList $field, $order) {
self::$dependenciesBuffer[$gid][] = [
'field' => $field,
'order' => $order,
];
}
/**
* 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();
if (array_key_exists($gid, self::$dependenciesBuffer)) {
foreach (self::$dependenciesBuffer[$gid] as $parent) {
$parent['field'][$parent['order']] = $entity;
}
}
}
}
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 | |
EntityReferenceFieldItemListProcessor:: |
protected static | property | ||
EntityReferenceFieldItemListProcessor:: |
public | function | Add dependencies to importing data. | |
EntityReferenceFieldItemListProcessor:: |
public | function |
Return export data array. Overrides TypeProcessorBase:: |
|
EntityReferenceFieldItemListProcessor:: |
public | function |
Init the $propertyId value in the entity to import. Overrides TypeProcessorBase:: |
|
EntityReferenceFieldItemListProcessor:: |
public | function | Action on Entity import end. | |
EntityReferenceFieldItemListProcessor:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides TypeProcessorBase:: |
|
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. | |
TypeProcessorBase:: |
protected | property | The entity processor plugin manager. | |
TypeProcessorBase:: |
protected | property | The global reference manager service. |