You are here

public function WebformCliService::drush_webform_purge in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Commands/WebformCliService.php \Drupal\webform\Commands\WebformCliService::drush_webform_purge()

Implements drush_hook_COMMAND().

Overrides WebformCliServiceInterface::drush_webform_purge

File

src/Commands/WebformCliService.php, line 464

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

public function drush_webform_purge($webform_id = NULL) {
  if ($this
    ->drush_get_option('all')) {
    $webform_id = 'all';
  }
  if (!$webform_id) {
    $webforms = array_keys(Webform::loadMultiple());
    $choices = array_combine($webforms, $webforms);
    $choices = array_merge([
      'all' => 'all',
    ], $choices);
    $webform_id = $this
      ->drush_choice($choices, $this
      ->dt("Choose a webform to purge submissions from."));
    if ($webform_id === FALSE) {
      return $this
        ->drush_user_abort();
    }
  }

  // Set the webform.
  $webform = $webform_id === 'all' ? NULL : Webform::load($webform_id);

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = \Drupal::service('entity_type.manager');

  /** @var \Drupal\webform\WebformSubmissionStorageInterface $submission_storage */
  $submission_storage = $entity_type_manager
    ->getStorage('webform_submission');

  // Make sure there are submissions that need to be deleted.
  if (!$submission_storage
    ->getTotal($webform)) {
    $this
      ->drush_print($this
      ->dt('There are no submissions that need to be deleted.'));
    return;
  }
  if (!$webform) {
    $submission_total = \Drupal::entityQuery('webform_submission')
      ->count()
      ->accessCheck(FALSE)
      ->execute();
    $form_total = \Drupal::entityQuery('webform')
      ->count()
      ->execute();
    $t_args = [
      '@submission_total' => $submission_total,
      '@submissions' => \Drupal::translation()
        ->formatPlural($submission_total, 'submission', 'submissions'),
      '@form_total' => $form_total,
      '@forms' => \Drupal::translation()
        ->formatPlural($form_total, 'webform', 'webforms'),
    ];
    if (!$this
      ->drush_confirm($this
      ->dt('Are you sure you want to delete @submission_total @submissions in @form_total @forms?', $t_args))) {
      return $this
        ->drush_user_abort();
    }
    $form = WebformResultsClearForm::create(\Drupal::getContainer());
    $form
      ->batchSet();
    $this
      ->drush_backend_batch_process();
  }
  else {

    // Set source entity.
    $entity_type = $this
      ->drush_get_option('entity-type');
    $entity_id = $this
      ->drush_get_option('entity-id');
    $source_entity = $entity_type && $entity_id ? \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id) : NULL;
    $t_args = [
      '@title' => $source_entity ? $source_entity
        ->label() : $webform
        ->label(),
    ];
    if (!$this
      ->drush_confirm($this
      ->dt("Are you sure you want to delete all submissions from '@title' webform?", $t_args))) {
      return $this
        ->drush_user_abort();
    }
    $form = WebformSubmissionsPurgeForm::create(\Drupal::getContainer());
    $form
      ->batchSet($webform, $source_entity);
    $this
      ->drush_backend_batch_process();
  }
}