You are here

protected function EntityShareCronService::filterDataToImport in Entity Share Cron 8.2

Same name and namespace in other branches
  1. 8 src/EntityShareCronService.php \Drupal\entity_share_cron\EntityShareCronService::filterDataToImport()
  2. 3.0.x src/EntityShareCronService.php \Drupal\entity_share_cron\EntityShareCronService::filterDataToImport()

Filters the data to import by existing or non existing entities.

Parameters

array $data: The data to be filtered.

bool $keep_existing: Keeps only existing entities if TRUE. Otherwise, only non existing entities.

1 call to EntityShareCronService::filterDataToImport()
EntityShareCronService::sync in src/EntityShareCronService.php
Synchronizes entities starting from provided channel URL.

File

src/EntityShareCronService.php, line 187

Class

EntityShareCronService
Entity Share Cron service.

Namespace

Drupal\entity_share_cron

Code

protected function filterDataToImport(array &$data, $keep_existing = TRUE) {

  // Groups entities by entity type before checking.
  $uuid_by_type = [];
  foreach ($data as $entity_data) {
    $parsed_type = explode('--', $entity_data['type']);
    $entity_type = $parsed_type[0];
    $uuid = $entity_data['id'];
    $uuid_by_type[$entity_type][] = $uuid;
  }

  // Filters entities.
  $existing_uuids = [];
  foreach ($uuid_by_type as $entity_type => $uuids) {
    $definition = $this->entityTypeManager
      ->getDefinition($entity_type);
    $uuid_property = $definition
      ->getKey('uuid');

    // Gets existing entities from the list.
    $storage = $this->entityTypeManager
      ->getStorage($entity_type);
    $existing_entities = $storage
      ->loadByProperties([
      $uuid_property => $uuids,
    ]);

    // Adds existing UUIDs to list.
    foreach ($existing_entities as $entity) {
      $uuid = $entity
        ->uuid();
      $existing_uuids[$uuid] = $uuid;
    }
  }

  // Filters data to be imported.
  $data_updated = [];
  foreach ($data as $entity_data) {
    $uuid = $entity_data['id'];
    if ($keep_existing && !empty($existing_uuids[$uuid])) {
      $data_updated[] = $entity_data;
    }
    elseif (!$keep_existing && empty($existing_uuids[$uuid])) {
      $data_updated[] = $entity_data;
    }
  }
  $data = $data_updated;
}