You are here

class EntityReferenceFieldItemListProcessor 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
  2. 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

Expanded class hierarchy of EntityReferenceFieldItemListProcessor

File

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

Namespace

Drupal\content_synchronizer\Plugin\content_synchronizer\type_processor
View 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\Entity $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(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);
          }
        }
      }
    }
  }

  /**
   * 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

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityReferenceFieldItemListProcessor::$dependenciesBuffer protected static property
EntityReferenceFieldItemListProcessor::addDependencie public function Add dependencies to importing data.
EntityReferenceFieldItemListProcessor::getExportedData public function Return export data array. Overrides TypeProcessorBase::getExportedData
EntityReferenceFieldItemListProcessor::initImportedEntity public function Init the $propertyId value in the entity to import. Overrides TypeProcessorBase::initImportedEntity
EntityReferenceFieldItemListProcessor::onImportedEntity public function Action on Entity import end.
EntityReferenceFieldItemListProcessor::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides TypeProcessorBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypeProcessorBase::$pluginManager protected property The entity processor plugin manager.
TypeProcessorBase::$referenceManager protected property The global reference manager service.