You are here

public function ContentHubExportQueueController::enqueueExportEntities in Acquia Content Hub 8

Adds entities to the export queue.

Parameters

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

Return value

\Drupal\Core\Entity\ContentEntityInterface[] An array of successfully queued entities (uuid, entity object).

File

src/Controller/ContentHubExportQueueController.php, line 88

Class

ContentHubExportQueueController
Implements an Export Queue Controller for Content Hub.

Namespace

Drupal\acquia_contenthub\Controller

Code

public function enqueueExportEntities(array $candidate_entities) {
  $queued_entities = [];

  // The collected entities are clean now and should all be processed.
  $exported_entities = [];
  foreach ($candidate_entities as $candidate_entity) {
    $entity_type = $candidate_entity
      ->getEntityTypeId();

    // Do not enqueue post-dependent entities like paragraphs.
    $post_dependency_types = ContentHubEntityDependency::getPostDependencyEntityTypes();
    if (in_array($entity_type, $post_dependency_types)) {
      continue;
    }
    $entity_id = $candidate_entity
      ->id();
    $entity_uuid = $candidate_entity
      ->uuid();
    $exported_entities[] = [
      'entity_type' => $entity_type,
      'entity_id' => $entity_id,
      'entity_uuid' => $entity_uuid,
    ];
    $queued_entities[$entity_uuid] = $candidate_entity;
  }
  unset($candidate_entities);

  // Obtain the export queue.
  $queue = $this->queueFactory
    ->get('acquia_contenthub_export_queue');

  // Divide the list of entities into chunks to be processed in groups.
  $entities_per_item = $this->config
    ->get('export_queue_entities_per_item');
  $entities_per_item = $entities_per_item ?: 1;
  $chunks = array_chunk($exported_entities, $entities_per_item);
  foreach ($chunks as $entities_chunk) {
    $uuids = [];
    foreach ($entities_chunk as $entity_chunk) {
      $uuids[] = $entity_chunk['entity_uuid'];
      unset($entity_chunk['entity_uuid']);
    }
    $item = new \stdClass();
    $item->data = $entities_chunk;
    if ($queue
      ->createItem($item) === FALSE) {
      $messages = [];
      foreach ($uuids as $uuid) {
        $message = new TranslatableMarkup('(Type = @type, ID = @id, UUID = @uuid)', [
          '@type' => $queued_entities[$uuid]
            ->getEntityTypeId(),
          '@id' => $queued_entities[$uuid]
            ->id(),
          '@uuid' => $uuid,
        ]);
        $messages[] = $message
          ->jsonSerialize();
        unset($queued_entities[$uuid]);
      }
      $this->logger
        ->debug('There was an error trying to enqueue the following entities for export: @entities.', [
        '@entities' => implode(', ', $messages),
      ]);
    }
  }
  return $queued_entities;
}