View source
<?php
namespace Drupal\webform\Form;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\EntityStorage\WebformEntityStorageTrait;
use Drupal\webform\WebformInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class WebformSubmissionsDeleteFormBase extends WebformDeleteFormBase {
use WebformEntityStorageTrait;
protected $submissionTotal;
protected $batchLimit = 1000;
protected $webform;
protected $sourceEntity;
protected $entityTypeManager;
protected $requestHandler;
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->entityTypeManager = $container
->get('entity_type.manager');
$instance->requestHandler = $container
->get('webform.request');
list($instance->webform, $instance->sourceEntity) = $instance->requestHandler
->getWebformEntities();
return $instance;
}
public function getConfirmText() {
return $this
->t('Clear');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setRedirectUrl($this
->getCancelUrl());
if ($this
->getSubmissionStorage()
->getTotal($this->webform, $this->sourceEntity) < $this
->getBatchLimit()) {
$this
->getSubmissionStorage()
->deleteAll($this->webform, $this->sourceEntity);
$this
->messenger()
->addStatus($this
->getFinishedMessage());
}
else {
$this
->batchSet($this->webform, $this->sourceEntity);
}
}
public function getLabel() {
if ($this->sourceEntity) {
return $this->sourceEntity
->label();
}
elseif ($this->webform
->label()) {
return $this->webform
->label();
}
else {
return '';
}
}
public function getFinishedMessage() {
return $this
->t('Webform submissions cleared.');
}
public function batchSet(WebformInterface $webform = NULL, EntityInterface $entity = NULL) {
$parameters = [
$webform,
$entity,
$this
->getSubmissionStorage()
->getMaxSubmissionId($webform, $entity),
];
$batch = [
'title' => $this
->t('Clear submissions'),
'init_message' => $this
->t('Clearing submission data'),
'error_message' => $this
->t('The submissions could not be cleared because an error occurred.'),
'operations' => [
[
[
$this,
'batchProcess',
],
$parameters,
],
],
'finished' => [
$this,
'batchFinish',
],
];
batch_set($batch);
}
public function getBatchLimit() {
return $this
->config('webform.settings')
->get('batch.default_batch_delete_size') ?: $this->batchLimit;
}
public function batchProcess(WebformInterface $webform = NULL, EntityInterface $entity = NULL, $max_sid, &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $this
->getSubmissionStorage()
->getTotal($webform, $entity, NULL, [
'in_draft' => NULL,
]);
$context['results']['webform'] = $webform;
$context['results']['entity'] = $entity;
}
$context['sandbox']['progress'] += $this
->getSubmissionStorage()
->deleteAll($webform, $entity, $this
->getBatchLimit(), $max_sid);
$context['message'] = $this
->t('Deleting @count of @total submissions…', [
'@count' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
]);
if ($context['sandbox']['progress'] !== $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public function batchFinish($success = FALSE, array $results = [], array $operations = []) {
if (!$success) {
$this
->messenger()
->addStatus($this
->t('Finished with an error.'));
}
else {
$this
->messenger()
->addStatus($this
->getFinishedMessage());
}
}
protected function getSubmissionTotal() {
if (!isset($this->submissionTotal)) {
$this->submissionTotal = $this
->getSubmissionStorage()
->getTotal($this->webform, $this->sourceEntity, NULL, [
'in_draft' => NULL,
]);
}
return $this->submissionTotal;
}
}