public function DeleteAllCommands::allDeleteEntities in Delete all 8
Same name and namespace in other branches
- 2.x src/Commands/DeleteAllCommands.php \Drupal\delete_all\Commands\DeleteAllCommands::allDeleteEntities()
Delete entities.
@option type pick entity type @option bundle pick entity bundle @usage drush delete-all-delete-entities Delete entities.
@command delete:all-delete-entities @aliases dade,delete-all-delete-entities
Parameters
array $options: An associative array of options whose values come from cli, aliases, config, etc.
File
- src/
Commands/ DeleteAllCommands.php, line 203
Class
- DeleteAllCommands
- A Drush commandfile.
Namespace
Drupal\delete_all\CommandsCode
public function allDeleteEntities(array $options = [
'type' => NULL,
'bundle' => NULL,
]) {
// drush_delete_all_delete_entities($options['type'], $options['bundle']);
// See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a
// legacy command.
// Get complete list of content entity types.
$entities_info = [];
$entities_info_extended = [];
$entity_type_options = FALSE;
$bundle_type_options = FALSE;
foreach (\Drupal::entityTypeManager()
->getDefinitions() as $id => $definition) {
if (is_a($definition, 'Drupal\\Core\\Entity\\ContentEntityType')) {
$entities_info[$id] = $definition
->getLabel();
$entities_info_extended[$id] = [
'label' => $definition
->getLabel(),
'entity_key_id' => $definition
->getKeys()['id'],
'entity_bundle' => $definition
->getKeys()['bundle'],
];
}
}
$deleteEntity = new EntityDeleteController();
// Get variables.
$vars = func_get_args();
// Check for presence of '--type' in drush command.
//
echo drush_get_option('type');
if ($options['type']) {
$entity_type_options = $options['type'];
if (!in_array($entity_type_options, array_keys($entities_info))) {
drush_set_error('Please select a valid entity type');
return;
}
}
if (!$entity_type_options) {
$entity_type_options = $this
->io()
->choice(dt("Choose an entity type to delete. All items of this"), $entities_info);
// Return if no entity is chosen or entity invalid.
if (!in_array($entity_type_options, array_keys($entities_info))) {
return;
}
}
$bundles_info = [
'all' => 'All',
];
$bundle_definitions = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_options);
if ($bundle_definitions) {
foreach ($bundle_definitions as $id => $definition) {
$bundles_info[$id] = $definition['label'];
}
// Check for presence of '--bundle' in drush command.
if (drush_get_option('bundle')) {
if ($vars && isset($vars[1])) {
$bundle_type_options = $vars[1];
if (!in_array($bundle_type_options, array_keys($bundles_info))) {
throw new UserAbortException('Please select a valid bundle type');
return;
}
}
}
if (!$bundle_type_options) {
$bundle_type_options = $this
->io()
->choice(dt("Choose bundle type to delete. All items of this"), $bundles_info);
if (!$bundle_type_options) {
return;
}
// Delete all if bundle is All.
if ($bundle_type_options == 'all') {
$bundle_type_options = FALSE;
}
}
}
if ($this
->io()
->confirm('Are you sure you want to delete the entities?')) {
// Get entities to delete.
$entities_to_delete = $deleteEntity
->getEntitiesToDelete($entity_type_options, $bundle_type_options, $entities_info_extended);
if ($entity_type_options == 'user') {
$entities_to_delete = array_diff($entities_to_delete, [
0,
1,
]);
}
// Get batch array.
$batch = $deleteEntity
->getEntitiesDeleteBatch($entities_to_delete, $entity_type_options);
// Initialize the batch.
batch_set($batch);
// Start the batch process.
drush_backend_batch_process();
}
else {
throw new UserAbortException();
}
}