You are here

public function ContentImportTrait::deleteContent in Content Synchronization 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/ContentImportTrait.php \Drupal\content_sync\Form\ContentImportTrait::deleteContent()

Processes the content import to be deleted or created batch and persists the importer.

Parameters

$content_to_sync:

string $serializer_context:

array $context: The batch context.

File

src/Form/ContentImportTrait.php, line 116

Class

ContentImportTrait
Defines the content import form.

Namespace

Drupal\content_sync\Form

Code

public function deleteContent(array $content_to_delete, $serializer_context = [], &$context) {
  if (empty($context['sandbox'])) {
    $directory = $serializer_context['content_sync_directory_entities'];
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['queue'] = $content_to_delete;
    $context['sandbox']['directory'] = $directory;
    $context['sandbox']['max'] = count($content_to_delete);
  }
  if (!empty($context['sandbox']['queue'])) {
    $error = TRUE;
    $item = array_pop($context['sandbox']['queue']);
    $ids = explode('.', $item);
    list($entity_type_id, $bundle, $uuid) = $ids;
    $entity = $this->contentSyncManager
      ->getEntityTypeManager()
      ->getStorage($entity_type_id)
      ->loadByProperties([
      'uuid' => $uuid,
    ]);
    $entity = array_shift($entity);
    if (!empty($entity)) {

      // Prevent Anonymous User and Super Admin from being deleted.
      if ($entity_type_id == 'user' && ((int) $entity
        ->id() === 0 || (int) $entity
        ->id() === 1)) {
        $message = $this
          ->t('@uuid - Anonymous user or super admin can not be removed.', [
          '@entity_type' => $entity_type_id,
          '@uuid' => $uuid,
        ]);
      }
      else {
        try {
          $message = $this
            ->t('Deleted content @label (@entity_type: @id).', [
            '@label' => $entity
              ->label(),
            '@id' => $entity
              ->id(),
            '@entity_type' => $entity
              ->getEntityTypeId(),
          ]);
          $entity
            ->delete();
          $error = FALSE;

          // Invalidate the CS Cache of the entity.
          $bundle = $entity
            ->bundle();
          $name = $entity_type_id . "." . $bundle . "." . $entity
            ->uuid();
          $cache = \Drupal::cache('content')
            ->invalidate($entity_type_id . "." . $bundle . ":" . $name);
        } catch (EntityStorageException $e) {
          $message = $e
            ->getMessage();
          \Drupal::messenger()
            ->addError($message);
        }
      }
    }
    else {
      $message = $this
        ->t('@uuid of type @entity_type was not found.', [
        '@entity_type' => $entity_type_id,
        '@uuid' => $uuid,
      ]);
    }
  }
  $context['results'][] = TRUE;
  $context['sandbox']['progress']++;
  $context['message'] = $message;
  if ($error) {
    if (!isset($context['results']['errors'])) {
      $context['results']['errors'] = [];
    }
    $context['results']['errors'][] = $context['message'];
  }
  $context['finished'] = $context['sandbox']['max'] > 0 && $context['sandbox']['progress'] < $context['sandbox']['max'] ? $context['sandbox']['progress'] / $context['sandbox']['max'] : 1;
}