You are here

public function ContentHubEntityExportController::exportEntities in Acquia Content Hub 8

Export entities to Content Hub (using the queue if enabled).

Parameters

\Drupal\Core\Entity\ContentEntityInterface[] $candidate_entities: An array of entities (uuid, entity object) to be exported to Content Hub.

Return value

bool TRUE if we are using the export queue, FALSE otherwise.

Throws

\Drupal\Core\Entity\EntityStorageException

File

src/Controller/ContentHubEntityExportController.php, line 158

Class

ContentHubEntityExportController
Controller for Content Hub Export Entities using bulk upload.

Namespace

Drupal\acquia_contenthub\Controller

Code

public function exportEntities(array $candidate_entities) {
  $candidate_entities = array_filter($candidate_entities);
  if ($this->exportQueueEnabled) {

    // These entities that reacted to a hook should always be re-exported,
    // then mark them as "queued" in the tracking table so they do not get
    // confused with exported entities (even if a previous version of the
    // entity has been previously exported).
    // This is fine because they will change back to exported status in the
    // tracking table after the queue runs, but we want to make sure that
    // these entities are taken again when collecting dependencies.
    // Dependencies are not exported if they have an entry in the tracking
    // table that says that they have been previously "exported".
    $this->exportQueueController
      ->enqueueExportEntities($candidate_entities);
    foreach ($candidate_entities as $candidate_entity) {
      $this
        ->queueExportedEntity($candidate_entity);
    }
    return TRUE;
  }
  $exported_entities = [];
  $bulk_url_array = [];
  foreach ($candidate_entities as $candidate_entity) {
    $entity_type = $candidate_entity
      ->getEntityTypeId();
    $entity_id = $candidate_entity
      ->id();
    $bulk_url_array[$entity_type][$entity_id] = $entity_id;
    $context['query_params']['include_references'] = 'true';
    $exported_entity = $this->entityCdfNormalizer
      ->normalize($candidate_entity, 'acquia_contenthub_cdf', $context);
    $exported_entity['entities'] = !empty($exported_entity) && is_array($exported_entity['entities']) ? $exported_entity['entities'] : [];
    foreach ($exported_entity['entities'] as $key => $ch_entity) {
      $exported_entity['entities'][$key] = Json::decode($ch_entity
        ->json());
    }
    $exported_entities = array_merge($exported_entities, $exported_entity['entities']);
  }

  // Eliminate duplicates.
  $exported_cdfs = [];
  foreach ($exported_entities as $cdf) {
    $exported_cdfs[$cdf['uuid']] = $cdf;
  }

  // Now implode parameters.
  foreach ($bulk_url_array as $entity_type => $entities) {
    $bulk_url_array[$entity_type] = implode(',', $entities);
  }
  $resource_url = $this->entityManager
    ->getBulkResourceUrl($bulk_url_array);
  if (!empty($exported_cdfs)) {
    if ($this->entityManager
      ->updateRemoteEntities($resource_url) !== FALSE) {

      // Setting up INITIATED status to all tracked exported entities.
      foreach ($exported_cdfs as $exported_entity) {

        // Obtaining the entity ID from the entity.
        $this
          ->trackExportedEntity($exported_entity);
      }
    }
  }

  // Log list of UUIDs being exported.
  $log_message = 'Drupal sending export request to Content Hub for UUIDs @uuids.';
  $context = [
    '@uuids' => implode(', ', array_keys($exported_cdfs)),
  ];
  $this->loggerFactory
    ->get('acquia_contenthub')
    ->debug($log_message, $context);
  return FALSE;
}