You are here

class DeleteContentCommand in Tome 8

Contains the tome:delete-content command.

@internal

Hierarchy

Expanded class hierarchy of DeleteContentCommand

1 string reference to 'DeleteContentCommand'
tome_sync.services.yml in modules/tome_sync/tome_sync.services.yml
modules/tome_sync/tome_sync.services.yml
1 service uses DeleteContentCommand
tome_sync.delete_content_command in modules/tome_sync/tome_sync.services.yml
Drupal\tome_sync\Commands\DeleteContentCommand

File

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

Namespace

Drupal\tome_sync\Commands
View source
class DeleteContentCommand extends ImportCommand {

  /**
   * The config installer.
   *
   * @var \Drupal\Core\Config\ConfigInstallerInterface
   */
  protected $configInstaller;

  /**
   * Constructs an DeleteContentCommand instance.
   *
   * @param \Drupal\tome_sync\ImporterInterface $importer
   *   The importer.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state system.
   * @param \Drupal\Core\Config\ConfigInstallerInterface $config_installer
   *   The config installer.
   */
  public function __construct(ImporterInterface $importer, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, ConfigInstallerInterface $config_installer) {
    parent::__construct($importer, $entity_type_manager, $state);
    $this->configInstaller = $config_installer;
  }

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    $this
      ->setName('tome:delete-content')
      ->setDescription('Deletes or removes translations for the given content.')
      ->addArgument('chunk', InputArgument::REQUIRED, 'A comma separated list of content names in the format entity_type_id:id or entity_type_id:id:langcode.');
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommandBase::$executable protected property The current executable path.
CommandBase::$io protected property The IO decorator.
CommandBase::initialize protected function
CommandBase::io protected function Returns the IO decorator, for reporting errors. Overrides ProcessTrait::io
DeleteContentCommand::$configInstaller protected property The config installer.
DeleteContentCommand::configure protected function Overrides ImportCommand::configure
DeleteContentCommand::execute public function Overrides ImportCommand::execute
DeleteContentCommand::__construct public function Constructs an DeleteContentCommand instance. Overrides ImportCommand::__construct
ExecutableFinderTrait::findExecutable protected function Finds an executable string for the current process.
ImportCommand::$entityTypeManager protected property The entity type manager.
ImportCommand::$importer protected property The importer.
ImportCommand::$state protected property The state system.
ImportCommand::checkImportingState protected function Checks the importing state and prompts the user if applicable.
ImportCommand::deleteContent protected function Deletes content using sub-processes.
ImportCommand::ENTITY_COUNT constant The default number of entities to import in each process.
ImportCommand::importChunks protected function Imports chunks of content using sub-processes.
ImportCommand::prepareConfigForImport protected function Prepares config for import by copying some directly from the source.
ImportCommand::PROCESS_COUNT constant The default number of processes to invoke.
ProcessTrait::displayErrors protected function Displays errors using the IO component.
ProcessTrait::runCommand protected function Runs a single command and outputs errors if encountered.
ProcessTrait::runCommands protected function Runs commands with concurrency.