You are here

public function CliService::force_entity_deletion in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x modules/cms_content_sync_developer/src/Cli/CliService.php \Drupal\cms_content_sync_developer\Cli\CliService::force_entity_deletion()
  2. 2.0.x modules/cms_content_sync_developer/src/Cli/CliService.php \Drupal\cms_content_sync_developer\Cli\CliService::force_entity_deletion()

Force the deletion of an entities and skip the syndication.

Parameters

$io:

$entity_type:

$options:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

\Drush\Exceptions\UserAbortException

File

modules/cms_content_sync_developer/src/Cli/CliService.php, line 52

Class

CliService

Namespace

Drupal\cms_content_sync_developer\Cli

Code

public function force_entity_deletion($io, $entity_type, $options) {
  self::$forceEntityDeletion = TRUE;
  $bundle = $options['bundle'];
  $entity_uuid = $options['entity_uuid'];
  if (isset($bundle) && isset($entity_uuid) || !isset($bundle) && !isset($entity_uuid)) {
    $io
      ->error('Either the bundle OR the entity_uuid option must be set.');
    return;
  }
  if (isset($entity_uuid)) {
    $entity = \Drupal::service('entity.repository')
      ->loadEntityByUuid($entity_type, $entity_uuid);
    if (!$entity) {
      $io
        ->error('An entity of type ' . $entity_type . ' having the uuid ' . $entity_uuid . ' does not exist.');
      return;
    }
    if (!$io
      ->confirm(dt('Do you really want to delete the entity of type ' . $entity_type . ' having the uuid: ' . $entity_uuid . ' '))) {
      throw new UserAbortException();
    }
    $entity
      ->delete();
    $io
      ->success('The ' . $entity_type . ' having the uuid ' . $entity_uuid . ' has been deleted.');
    return;
  }
  if (isset($bundle)) {
    if (!$io
      ->confirm(dt('Do you really want to delete all entities of the type: ' . $entity_type . ' having the bundle: ' . $bundle . ' ?'))) {
      throw new UserAbortException();
    }
    $bundle_key = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->getEntityType()
      ->getKey('bundle');
    if ($entity_type == 'menu_link_content') {
      $bundle_key = 'menu_name';
    }
    $entities = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->loadByProperties([
      $bundle_key => $bundle,
    ]);
    foreach ($entities as $entity) {
      $entity
        ->delete();
    }
    $io
      ->success('All entities of type: ' . $entity_type . ' and bundle: ' . $bundle . ' have been deleted.');
    return;
  }
}