You are here

protected function EntityReference::getExistingEntities in Entity Share 8.3

Helper function to get existing reference entities.

Parameters

array $data: The JSON:API data for an entity reference field.

Return value

array An array of entity IDs keyed by UUID.

1 call to EntityReference::getExistingEntities()
EntityReference::processEntity in modules/entity_share_client/src/Plugin/EntityShareClient/Processor/EntityReference.php
Method called on STAGE_PROCESS_ENTITY.

File

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

Class

EntityReference
Handle entity reference.

Namespace

Drupal\entity_share_client\Plugin\EntityShareClient\Processor

Code

protected function getExistingEntities(array $data) {
  $referenced_entities_ids = [];
  $entity_uuids = [];

  // Extract list of UUIDs.
  foreach ($data as $field_value_data) {
    if ($field_value_data['id'] !== 'missing') {
      $parsed_type = explode('--', $field_value_data['type']);
      $entity_type_id = $parsed_type[0];
      $entity_uuids[] = $field_value_data['id'];
    }
  }
  if (!empty($entity_uuids)) {
    try {

      // Load the entities to be able to return an array of IDs keyed by
      // UUIDs. Sorting the array will be done later.
      $entity_storage = $this->entityTypeManager
        ->getStorage($entity_type_id);
      $existing_entity_ids = $entity_storage
        ->getQuery()
        ->condition('uuid', $entity_uuids, 'IN')
        ->execute();
      $existing_entities = $entity_storage
        ->loadMultiple($existing_entity_ids);
      foreach ($existing_entities as $existing_entity) {
        $referenced_entities_ids[$existing_entity
          ->uuid()] = $existing_entity
          ->id();
      }
    } catch (\Exception $e) {
      $log_variables = [];
      $log_variables['@msg'] = $e
        ->getMessage();
      $this->logger
        ->error('Caught exception trying to load existing entities. Error message was @msg', $log_variables);
    }
  }
  return $referenced_entities_ids;
}