You are here

public function WebformCliService::drush_webform_remove_orphans in Webform 8.5

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

See also

\Drupal\webform\Form\AdminConfig\WebformAdminConfigAdvancedForm::submitForm

File

src/Commands/WebformCliService.php, line 890

Class

WebformCliService
Drush version agnostic commands.

Namespace

Drupal\webform\Commands

Code

public function drush_webform_remove_orphans() {
  $webform_ids = [];
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('webform.webform.') as $webform_config_name) {
    $webform_id = str_replace('webform.webform.', '', $webform_config_name);
    $webform_ids[$webform_id] = $webform_id;
  }
  $sids = \Drupal::database()
    ->select('webform_submission')
    ->fields('webform_submission', [
    'sid',
  ])
    ->condition('webform_id', $webform_ids, 'NOT IN')
    ->orderBy('sid')
    ->execute()
    ->fetchCol();
  if (!$sids) {
    $this
      ->drush_print($this
      ->dt('No orphaned submission found.'));
    return;
  }
  $t_args = [
    '@total' => count($sids),
  ];
  if (!$this
    ->drush_confirm($this
    ->dt("Are you sure you want remove @total orphaned webform submissions?", $t_args))) {
    return $this
      ->drush_user_abort();
  }
  $this
    ->drush_print($this
    ->dt('Deleting @total orphaned webform submissions…', $t_args));
  $submissions = WebformSubmission::loadMultiple($sids);
  foreach ($submissions as $submission) {
    $submission
      ->delete();
  }
}