You are here

function acquia_contenthub_subscriber_track_imported_unlinked_entities in Acquia Content Hub 8.2

Stores imported entities with local changes to a state variable.

Parameters

mixed $context: The context array.

1 string reference to 'acquia_contenthub_subscriber_track_imported_unlinked_entities'
AcquiaContentHubSubscriberCommands::upgrade in modules/acquia_contenthub_subscriber/src/Commands/AcquiaContentHubSubscriberCommands.php
Subscriber Upgrade Command.

File

modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.migrate.inc, line 99
Post-update functions for Content Hub Subscriber.

Code

function acquia_contenthub_subscriber_track_imported_unlinked_entities(&$context) {
  $database = \Drupal::database();
  if (!$database
    ->schema()
    ->tableExists('acquia_contenthub_entities_tracking')) {
    return;
  }
  $logger = \Drupal::logger('acquia_contenthub_subscriber');
  $entity_type_manager = \Drupal::entityTypeManager();
  $import_status_no_auto_update = [
    'AUTO_UPDATE_DISABLED',
    'HAS_LOCAL_CHANGE',
  ];
  $query = $database
    ->select('acquia_contenthub_entities_tracking', 't')
    ->fields('t', [
    'entity_id',
    'entity_uuid',
    'entity_type',
  ]);
  $query
    ->condition('status_import', $import_status_no_auto_update, 'IN');
  if (!isset($context['sandbox']['progress'])) {
    $max = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $context['sandbox']['progress'] = 0;
    $context['finished'] = 0;
    $context['sandbox']['max'] = $max;
  }
  $limit = 50;

  // Obtain the list of entities to enqueue.
  $results = $query
    ->range($context['sandbox']['progress'], $limit)
    ->execute();
  $unlinked = [];
  foreach ($results as $result) {
    $uuid = $result->entity_uuid;
    if ($entity = $entity_type_manager
      ->getStorage($result->entity_type)
      ->load($result->entity_id)) {
      $revision_id = NULL;
      if ($entity instanceof RevisionableInterface) {
        $revision_id = $entity
          ->getRevisionId() ?: NULL;
      }

      // Collect entity id, type and revision_id from all imported entities
      // with local changes or disconnected from Content Hub.
      $unlinked[$uuid] = [
        'id' => $result->entity_id,
        'uuid' => $result->entity_uuid,
        'type' => $result->entity_type,
        'revision_id' => $revision_id,
      ];
      $logger
        ->debug(sprintf('Entity disconnected from Content Hub: (id = %s, UUID = %s, type = %s, revision_id = %s)', $result->entity_id, $result->entity_uuid, $result->entity_type, $revision_id));
    }

    // Updating progress.
    $context['sandbox']['progress']++;
  }
  if (!empty($unlinked)) {
    $state = \Drupal::state();
    $disconnected_entities = $state
      ->get('acquia_contenthub_update_82001_disconnected_entities', []);
    $disconnected_entities = array_merge($disconnected_entities, $unlinked);
    $state
      ->set('acquia_contenthub_update_82001_disconnected_entities', $disconnected_entities);
  }

  // Did we finish yet?
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['max'] > 0 ? $context['sandbox']['progress'] / $context['sandbox']['max'] : 1;
  }
  else {
    $context['finished'] = 1;

    // Delete the unlinked entities from the tracking table.
    $database
      ->delete('acquia_contenthub_entities_tracking')
      ->condition('status_import', $import_status_no_auto_update, 'IN')
      ->execute();

    // Delete legacy tracking table if empty.
    $query = $database
      ->select('acquia_contenthub_entities_tracking', 't')
      ->fields('t', [
      'entity_id',
      'entity_uuid',
      'entity_type',
    ]);
    $count = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    if ($count == 0) {

      // If table is empty, drop it.
      $database
        ->schema()
        ->dropTable('acquia_contenthub_entities_tracking');
    }
  }
}