You are here

public static function RemoveGatherContentLocalData::cleanUpGcData in GatherContent 8.4

Clean GC IDs from node and file entity.

Parameters

string $entity_type_id: Entity type ID of which we want to clean entities.

array $context: Array of context.

File

gathercontent_ui/src/Form/RemoveGatherContentLocalData.php, line 102

Class

RemoveGatherContentLocalData
Class RemoveGatherContentLocalData.

Namespace

Drupal\gathercontent_ui\Form

Code

public static function cleanUpGcData($entity_type_id, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox']['num_of_precessed_items'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['total_count'] = \Drupal::entityQuery($entity_type_id)
      ->condition('gc_id', NULL, 'IS NOT NULL')
      ->count()
      ->execute();
  }
  $limit = 50;
  $id_key = \Drupal::entityTypeManager()
    ->getDefinition($entity_type_id)
    ->getKey('id');
  $entity_ids = \Drupal::entityQuery($entity_type_id)
    ->condition($id_key, $context['sandbox']['current_id'], '>')
    ->condition('gc_id', NULL, 'IS NOT NULL')
    ->sort($id_key)
    ->range(0, $limit)
    ->execute();
  foreach ($entity_ids as $entity_id) {
    $context['sandbox']['num_of_precessed_items']++;
    $context['sandbox']['current_id'] = $entity_id;

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type_id)
      ->load($entity_id);
    $entity
      ->set('gc_id', NULL);
    if ($entity
      ->hasField('gc_mapping_id')) {
      $entity
        ->set('gc_mapping_id', NULL);
    }
    $entity
      ->save();
  }
  if ($context['sandbox']['num_of_precessed_items'] != $context['sandbox']['total_count']) {
    $context['finished'] = $context['sandbox']['num_of_precessed_items'] / $context['sandbox']['total_count'];
  }
}