class DeleteContentCommand in Tome 8
Contains the tome:delete-content command.
@internal
Hierarchy
- class \Drupal\tome_base\CommandBase extends \Symfony\Component\Console\Command\Command uses ExecutableFinderTrait, ProcessTrait
- class \Drupal\tome_sync\Commands\ImportCommand
- class \Drupal\tome_sync\Commands\DeleteContentCommand
- class \Drupal\tome_sync\Commands\ImportCommand
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
File
- modules/
tome_sync/ src/ Commands/ DeleteContentCommand.php, line 19
Namespace
Drupal\tome_sync\CommandsView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommandBase:: |
protected | property | The current executable path. | |
CommandBase:: |
protected | property | The IO decorator. | |
CommandBase:: |
protected | function | ||
CommandBase:: |
protected | function |
Returns the IO decorator, for reporting errors. Overrides ProcessTrait:: |
|
DeleteContentCommand:: |
protected | property | The config installer. | |
DeleteContentCommand:: |
protected | function |
Overrides ImportCommand:: |
|
DeleteContentCommand:: |
public | function |
Overrides ImportCommand:: |
|
DeleteContentCommand:: |
public | function |
Constructs an DeleteContentCommand instance. Overrides ImportCommand:: |
|
ExecutableFinderTrait:: |
protected | function | Finds an executable string for the current process. | |
ImportCommand:: |
protected | property | The entity type manager. | |
ImportCommand:: |
protected | property | The importer. | |
ImportCommand:: |
protected | property | The state system. | |
ImportCommand:: |
protected | function | Checks the importing state and prompts the user if applicable. | |
ImportCommand:: |
protected | function | Deletes content using sub-processes. | |
ImportCommand:: |
constant | The default number of entities to import in each process. | ||
ImportCommand:: |
protected | function | Imports chunks of content using sub-processes. | |
ImportCommand:: |
protected | function | Prepares config for import by copying some directly from the source. | |
ImportCommand:: |
constant | The default number of processes to invoke. | ||
ProcessTrait:: |
protected | function | Displays errors using the IO component. | |
ProcessTrait:: |
protected | function | Runs a single command and outputs errors if encountered. | |
ProcessTrait:: |
protected | function | Runs commands with concurrency. |