You are here

public function DeleteContentCommand::execute in Tome 8

Overrides ImportCommand::execute

File

modules/tome_sync/src/Commands/DeleteContentCommand.php, line 57

Class

DeleteContentCommand
Contains the tome:delete-content command.

Namespace

Drupal\tome_sync\Commands

Code

public function execute(InputInterface $input, OutputInterface $output) {
  $this->configInstaller
    ->setSyncing(TRUE);
  $this->importer
    ->isImporting(TRUE);
  $chunk = $input
    ->getArgument('chunk');
  $names = explode(',', $chunk);
  $storages = [];
  foreach ($names as $name) {
    $parts = explode(':', $name);
    $entity_type_id = $parts[0];
    $id = $parts[1];
    $langcode = isset($parts[2]) ? $parts[2] : NULL;
    $entity_type = $this->entityTypeManager
      ->getDefinition($entity_type_id);
    if (!$entity_type) {
      $this
        ->io()
        ->error("The entity type {$entity_type_id} does not exist.");
      return 1;
    }
    if (!isset($storages[$entity_type_id])) {
      $storages[$entity_type_id] = $this->entityTypeManager
        ->getStorage($entity_type_id);
    }
    $entity = $storages[$entity_type_id]
      ->load($id);
    if (!$entity) {
      $this
        ->io()
        ->error("No entity found for {$name}.");
      return 1;
    }
    if (!$entity instanceof ContentEntityInterface) {
      $this
        ->io()
        ->error("{$name} is not a content entity.");
      return 1;
    }
    if ($langcode) {
      if ($translation = $entity
        ->getTranslation($langcode)) {
        $entity
          ->removeTranslation($langcode);
        $entity
          ->save();
      }
      else {
        $this
          ->io()
          ->error("There is no {$langcode} translation for {$name}.");
        return 1;
      }
    }
    else {
      $entity
        ->delete();
    }
  }
  $this->importer
    ->isImporting(FALSE);
  $this->configInstaller
    ->setSyncing(FALSE);
}