You are here

public function EntityReference::processEntity in Entity Share 8.3

Method called on STAGE_PROCESS_ENTITY.

If the plugin reacts to this stage.

Parameters

\Drupal\entity_share_client\RuntimeImportContext $runtime_import_context: The import context.

\Drupal\Core\Entity\ContentEntityInterface $processed_entity: The entity being processed.

array $entity_json_data: The entity JSON data.

Overrides ImportProcessorPluginBase::processEntity

File

modules/entity_share_client/src/Plugin/EntityShareClient/Processor/EntityReference.php, line 115

Class

EntityReference
Handle entity reference.

Namespace

Drupal\entity_share_client\Plugin\EntityShareClient\Processor

Code

public function processEntity(RuntimeImportContext $runtime_import_context, ContentEntityInterface $processed_entity, array $entity_json_data) {
  if (isset($entity_json_data['relationships'])) {
    $field_mappings = $runtime_import_context
      ->getFieldMappings();

    // Loop on reference fields.
    foreach ($entity_json_data['relationships'] as $field_public_name => $field_data) {
      $field_internal_name = array_search($field_public_name, $field_mappings[$processed_entity
        ->getEntityTypeId()][$processed_entity
        ->bundle()]);
      if (!$processed_entity
        ->hasField($field_internal_name)) {
        $this->logger
          ->notice('Error during import. The field @field does not exist.', [
          '@field' => $field_internal_name,
        ]);
        continue;
      }
      $field = $processed_entity
        ->get($field_internal_name);
      if ($this->entityReferenceHelper
        ->relationshipHandleable($field) !== EntityReferenceHelperInterface::RELATIONSHIP_HANDLEABLE) {
        continue;
      }
      $main_property = $field
        ->getItemDefinition()
        ->getMainPropertyName();
      $field_values = [];

      // Check that the field has data.
      if ($field_data['data'] != NULL) {
        $prepared_field_data = EntityShareUtility::prepareData($field_data['data']);
        $referenced_entities_ids = [];

        // Max recursion depth reached. Reference only existing entities.
        if ($this->currentRecursionDepth == $this->configuration['max_recursion_depth']) {
          $referenced_entities_ids = $this
            ->getExistingEntities($prepared_field_data);
        }
        elseif (isset($field_data['links']['related']['href'])) {
          $referenced_entities_ids = $this
            ->importUrl($runtime_import_context, $field_data['links']['related']['href']);

          // It is possible that some entities have been skipped from import,
          // but do exist, so ensure that those are available to the
          // mapping code below.
          $referenced_entities_ids = $this
            ->getExistingEntities($prepared_field_data) + $referenced_entities_ids;
        }

        // Add field value.
        // As the loop is on the JSON:API data, the sort is preserved.
        foreach ($prepared_field_data as $field_value_data) {
          $referenced_entity_uuid = $field_value_data['id'];

          // Check that the referenced entity exists or had been imported.
          if (!isset($referenced_entities_ids[$referenced_entity_uuid])) {
            continue;
          }
          $field_value = [
            $main_property => $referenced_entities_ids[$referenced_entity_uuid],
          ];

          // Add field metadata.
          if (isset($field_value_data['meta'])) {
            $field_value += $field_value_data['meta'];
          }

          // Allow to alter the field value with an event.
          $event = new RelationshipFieldValueEvent($field, $field_value);
          $this->eventDispatcher
            ->dispatch(RelationshipFieldValueEvent::EVENT_NAME, $event);
          $field_values[] = $event
            ->getFieldValue();
        }
      }
      $processed_entity
        ->set($field_public_name, $field_values);
    }

    // @todo Test if this is still needed.
    // Save the entity once all the references have been updated.
    $processed_entity
      ->save();
  }
}