diff --git a/config/install/webform.settings.yml b/config/install/webform.settings.yml index f69fb6be..c462b71e 100644 --- a/config/install/webform.settings.yml +++ b/config/install/webform.settings.yml @@ -36,8 +36,6 @@ settings: button default_limit_total_message: 'No more submissions are permitted.' default_limit_user_message: 'No more submissions are permitted.' - default_purge: 'none' - default_purge_days: 14 assets: css: '' javascript: '' @@ -94,7 +92,7 @@ mail: default_body_text: | Submitted on [webform_submission:created] Submitted by: [webform_submission:user] - + Submitted values are: [webform_submission:values] default_body_html: | diff --git a/config/schema/webform.schema.yml b/config/schema/webform.schema.yml index dfd68cd2..60723a08 100644 --- a/config/schema/webform.schema.yml +++ b/config/schema/webform.schema.yml @@ -80,12 +80,6 @@ webform.settings: default_limit_user_message: type: text label: 'Default limit user message' - default_purge: - type: string - label: 'Default purging' - default_purge_days: - type: integer - label: 'Default days to retain submissions' form_classes: type: string label: 'Webform CSS classes' diff --git a/src/Entity/Webform.php b/src/Entity/Webform.php index f9d732da..ad6cd79a 100644 --- a/src/Entity/Webform.php +++ b/src/Entity/Webform.php @@ -18,6 +18,7 @@ use Drupal\webform\WebformHandlerInterface; use Drupal\webform\WebformHandlerPluginCollection; use Drupal\webform\WebformInterface; use Drupal\webform\WebformSubmissionInterface; +use Drupal\webform\WebformSubmissionStorageInterface; /** * Defines the webform entity. @@ -558,8 +559,8 @@ class Webform extends ConfigEntityBundleBase implements WebformInterface { 'limit_total_message' => '', 'limit_user' => NULL, 'limit_user_message' => '', - 'purge' => self::PURGE_NONE, - 'purge_days' => 14, + 'purge' => WebformSubmissionStorageInterface::PURGE_NONE, + 'purge_days' => NULL, 'entity_limit_total' => NULL, 'entity_limit_user' => NULL, 'results_disabled' => FALSE, diff --git a/src/Form/WebformAdminSettingsForm.php b/src/Form/WebformAdminSettingsForm.php index 8a352d82..acf3794d 100644 --- a/src/Form/WebformAdminSettingsForm.php +++ b/src/Form/WebformAdminSettingsForm.php @@ -344,30 +344,6 @@ class WebformAdminSettingsForm extends ConfigFormBase { '#default_value' => $config->get('settings.default_limit_user_message'), ]; - $form['purge'] = [ - '#type' => 'details', - '#title' => $this->t('Submission purging default settings'), - '#tree' => TRUE, - ]; - $form['purge']['default_purge'] = [ - '#type' => 'select', - '#title' => $this->t('Automatically purge'), - '#options' => [ - WebformInterface::PURGE_NONE => $this->t('None'), - WebformInterface::PURGE_DRAFT => $this->t('Draft'), - WebformInterface::PURGE_COMPLETED => $this->t('Completed'), - WebformInterface::PURGE_ALL => $this->t('Draft and completed'), - ], - '#default_value' => $config->get('settings.default_purge'), - ]; - $form['purge']['default_purge_days'] = [ - '#type' => 'number', - '#title' => $this->t('Days to retain submission'), - '#min' => 0, - '#default_value' => $config->get('settings.default_purge_days'), - '#required' => TRUE, - ]; - // Assets. $form['assets'] = [ '#type' => 'details', @@ -642,7 +618,7 @@ class WebformAdminSettingsForm extends ConfigFormBase { '#title' => $this->t('Amount of submissions to process'), '#min' => 1, '#default_value' => $config->get('purge_settings.cron_size'), - '#description' => $this->t('Amount of submissions to purge during single cron run. You may want to lower this number if you are facing memory or timeout issues when puring via cron.'), + '#description' => $this->t('Amount of submissions to purge during single cron run. You may want to lower this number if you are facing memory or timeout issues when purging via cron.'), ]; // Test. @@ -747,8 +723,7 @@ class WebformAdminSettingsForm extends ConfigFormBase { + $form_state->getValue('preview') + $form_state->getValue('draft') + $form_state->getValue('confirmation') - + $form_state->getValue('limit') - + $form_state->getValue('purge'); + + $form_state->getValue('limit'); // Trigger update all webform paths if the 'default_page_base_path' changed. $update_paths = ($settings['default_page_base_path'] != $this->config('webform.settings')->get('settings.default_page_base_path')) ? TRUE : FALSE; diff --git a/src/WebformEntitySettingsForm.php b/src/WebformEntitySettingsForm.php index 9d29e9de..c3402d08 100644 --- a/src/WebformEntitySettingsForm.php +++ b/src/WebformEntitySettingsForm.php @@ -662,19 +662,23 @@ class WebformEntitySettingsForm extends EntityForm { '#type' => 'select', '#title' => $this->t('Automatically purge'), '#options' => [ - WebformInterface::PURGE_NONE => $this->t('None'), - WebformInterface::PURGE_DRAFT => $this->t('Draft'), - WebformInterface::PURGE_COMPLETED => $this->t('Completed'), - WebformInterface::PURGE_ALL => $this->t('Draft and completed'), + WebformSubmissionStorageInterface::PURGE_NONE => $this->t('None'), + WebformSubmissionStorageInterface::PURGE_DRAFT => $this->t('Draft'), + WebformSubmissionStorageInterface::PURGE_COMPLETED => $this->t('Completed'), + WebformSubmissionStorageInterface::PURGE_ALL => $this->t('Draft and completed'), ], '#default_value' => $settings['purge'], ]; $form['purge']['purge_days'] = [ '#type' => 'number', - '#title' => $this->t('Days to retain submission'), - '#min' => 0, + '#title' => $this->t('Days to retain submissions'), + '#min' => 1, '#default_value' => $settings['purge_days'], - '#required' => TRUE, + '#states' => array( + 'visible' => array('select[name="purge"]' => array('!value' => WebformSubmissionStorageInterface::PURGE_NONE)), + 'required' => array('select[name="purge"]' => array('!value' => WebformSubmissionStorageInterface::PURGE_NONE)), + ), + '#field_suffix' => $this->t('days'), ]; // Confirmation. @@ -867,6 +871,22 @@ class WebformEntitySettingsForm extends EntityForm { /** * {@inheritdoc} */ + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); + if ($form_state->getValue('purge') != WebformSubmissionStorageInterface::PURGE_NONE) { + $purge_days = $form_state->getValue('purge_days'); + $element = $form['purge']['purge_days']; + if (!$purge_days) { + $form_state->setError($element, $this->t('%title is required.', [ + '%title' => $element['#title'], + ])); + } + } + } + + /** + * {@inheritdoc} + */ public function save(array $form, FormStateInterface $form_state) { $values = $form_state->getValues(); diff --git a/src/WebformInterface.php b/src/WebformInterface.php index ae770e7a..5378a572 100644 --- a/src/WebformInterface.php +++ b/src/WebformInterface.php @@ -13,34 +13,6 @@ use Drupal\user\EntityOwnerInterface; interface WebformInterface extends ConfigEntityInterface, EntityWithPluginCollectionInterface, EntityOwnerInterface { /** - * Denote not to purge automatically anything at all. - * - * @var string - */ - const PURGE_NONE = 'none'; - - /** - * Denote to purge automatically only drafts. - * - * @var string - */ - const PURGE_DRAFT = 'draft'; - - /** - * Denote to purge automatically only completed submissions. - * - * @var string - */ - const PURGE_COMPLETED = 'completed'; - - /** - * Denote to purge automatically all submissions. - * - * @var string - */ - const PURGE_ALL = 'all'; - - /** * Determine if the webform has page or is attached to other entities. * * @return bool diff --git a/src/WebformSubmissionPurge.php b/src/WebformSubmissionPurge.php deleted file mode 100644 index 4c1fd476..00000000 --- a/src/WebformSubmissionPurge.php +++ /dev/null @@ -1,77 +0,0 @@ -queryFactory = $query_factory; - $this->webformStorage = $entity_type_manager->getStorage('webform'); - $this->webformSubmissionStorage = $entity_type_manager->getStorage('webform_submission'); - } - - /** - * {@inheritdoc} - */ - public function purgeWebformSubmission($count) { - $days_to_seconds = 60 * 60 * 24; - - $query = $this->queryFactory->get('webform'); - $query->condition('settings.purge', WebformInterface::PURGE_NONE, '<>'); - $query->condition('settings.purge_days', 0, '>'); - $webforms_to_purge = array_values($query->execute()); - - $webform_submissions_to_purge = array(); - - if (!empty($webforms_to_purge)) { - $webforms_to_purge = $this->webformStorage->loadMultiple($webforms_to_purge); - foreach ($webforms_to_purge as $webform) { - $query = $this->queryFactory->get('webform_submission'); - $query->condition('created', REQUEST_TIME - ($webform->getSetting('purge_days') * $days_to_seconds), '<'); - $query->condition('webform_id', $webform->id()); - switch ($webform->getSetting('purge')) { - case WebformInterface::PURGE_DRAFT: - $query->condition('in_draft', TRUE); - break; - - case WebformInterface::PURGE_COMPLETED: - $query->condition('in_draft', FALSE); - break; - } - $query->range(0, $count - count($webform_submissions_to_purge)); - $result = array_values($query->execute()); - if (!empty($result)) { - $webform_submissions_to_purge = array_merge($webform_submissions_to_purge, $result); - } - if (count($webform_submissions_to_purge) == $count) { - // We've collected enough webform submissions for purging in this run. - break; - } - } - } - - if (!empty($webform_submissions_to_purge)) { - $webform_submissions_to_purge = $this->webformSubmissionStorage->loadMultiple($webform_submissions_to_purge); - $this->webformSubmissionStorage->delete($webform_submissions_to_purge); - } - } - -} diff --git a/src/WebformSubmissionPurgeInterface.php b/src/WebformSubmissionPurgeInterface.php deleted file mode 100644 index f0be3601..00000000 --- a/src/WebformSubmissionPurgeInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -entityManager->getStorage('webform')->getQuery(); + $query->condition('settings.purge', self::PURGE_NONE, '<>'); + $query->condition('settings.purge_days', 0, '>'); + $webforms_to_purge = array_values($query->execute()); + + $webform_submissions_to_purge = array(); + + if (!empty($webforms_to_purge)) { + $webforms_to_purge = $this->entityManager->getStorage('webform')->loadMultiple($webforms_to_purge); + foreach ($webforms_to_purge as $webform) { + $query = $this->getQuery(); + $query->condition('created', REQUEST_TIME - ($webform->getSetting('purge_days') * $days_to_seconds), '<'); + $query->condition('webform_id', $webform->id()); + switch ($webform->getSetting('purge')) { + case self::PURGE_DRAFT: + $query->condition('in_draft', TRUE); + break; + + case self::PURGE_COMPLETED: + $query->condition('in_draft', FALSE); + break; + } + $query->range(0, $count - count($webform_submissions_to_purge)); + $result = array_values($query->execute()); + if (!empty($result)) { + $webform_submissions_to_purge = array_merge($webform_submissions_to_purge, $result); + } + if (count($webform_submissions_to_purge) == $count) { + // We've collected enough webform submissions for purging in this run. + break; + } + } + } + + if (!empty($webform_submissions_to_purge)) { + $webform_submissions_to_purge = $this->loadMultiple($webform_submissions_to_purge); + $this->delete($webform_submissions_to_purge); + } + } + + /** + * {@inheritdoc} + */ protected function getTerminusSubmission(WebformInterface $webform, EntityInterface $source_entity = NULL, AccountInterface $account = NULL, $sort = 'DESC') { $query = $this->getQuery(); $query->condition('webform_id', $webform->id()); diff --git a/src/WebformSubmissionStorageInterface.php b/src/WebformSubmissionStorageInterface.php index 927bdd84..500a988e 100644 --- a/src/WebformSubmissionStorageInterface.php +++ b/src/WebformSubmissionStorageInterface.php @@ -17,6 +17,34 @@ interface WebformSubmissionStorageInterface extends ContentEntityStorageInterfac const SAVED_DISABLED = 0; /** + * Denote not to purge automatically anything at all. + * + * @var string + */ + const PURGE_NONE = 'none'; + + /** + * Denote to purge automatically only drafts. + * + * @var string + */ + const PURGE_DRAFT = 'draft'; + + /** + * Denote to purge automatically only completed submissions. + * + * @var string + */ + const PURGE_COMPLETED = 'completed'; + + /** + * Denote to purge automatically all submissions. + * + * @var string + */ + const PURGE_ALL = 'all'; + + /** * Get webform submission entity field definitions. * * The helper method is generally used for exporting results. @@ -177,6 +205,14 @@ interface WebformSubmissionStorageInterface extends ContentEntityStorageInterfac public function getSourceEntityTypes(WebformInterface $webform); /** + * Purge webform submissions. + * + * @param int $count + * Amount of webform submissions to purge + */ + public function purgeWebformSubmission($count); + + /** * Get customized submission columns used to display custom table. * * @param \Drupal\webform\WebformInterface|null $webform diff --git a/tests/src/Kernel/WebformSubmissionPurgeTest.php b/tests/src/Kernel/WebformSubmissionPurgeTest.php index 3bd740f4..2e1f72e1 100644 --- a/tests/src/Kernel/WebformSubmissionPurgeTest.php +++ b/tests/src/Kernel/WebformSubmissionPurgeTest.php @@ -65,7 +65,7 @@ class WebfromSubmissionPurgeTest extends KernelTestBase { $webform_no_purging = Webform::create([ 'id' => $this->randomMachineName(), ]); - $webform_no_purging->setSetting('purge', WebformInterface::PURGE_NONE); + $webform_no_purging->setSetting('purge', WebformSubmissionStorageInterface::PURGE_NONE); $webform_no_purging->save(); foreach ($webform_submissions_definition as $definition) { @@ -79,7 +79,7 @@ class WebfromSubmissionPurgeTest extends KernelTestBase { } } - \Drupal::service('webform.purge')->purgeWebformSubmission(self::WEBFORM_PURGE_AMOUNT); + \Drupal::entityTypeManager()->getStorage('webform_submission')->purgeWebformSubmission(self::WEBFORM_PURGE_AMOUNT); // Make sure nothing has been purged in the webform where purging is // disabled. @@ -119,7 +119,7 @@ class WebfromSubmissionPurgeTest extends KernelTestBase { // Test that only drafts are purged. $tests[] = [ - WebformInterface::PURGE_DRAFT, + WebformSubmissionStorageInterface::PURGE_DRAFT, [ [TRUE, TRUE], [TRUE, FALSE], @@ -131,7 +131,7 @@ class WebfromSubmissionPurgeTest extends KernelTestBase { // Test that only completed submissions are purged. $tests[] = [ - WebformInterface::PURGE_COMPLETED, + WebformSubmissionStorageInterface::PURGE_COMPLETED, [ [TRUE, TRUE], [TRUE, FALSE], @@ -143,7 +143,7 @@ class WebfromSubmissionPurgeTest extends KernelTestBase { // Test that both completed and draft submissions are purged. $tests[] = [ - WebformInterface::PURGE_ALL, + WebformSubmissionStorageInterface::PURGE_ALL, [ [TRUE, TRUE], [TRUE, FALSE], @@ -155,7 +155,7 @@ class WebfromSubmissionPurgeTest extends KernelTestBase { // Test the cron size parameter. $tests[] = [ - WebformInterface::PURGE_ALL, + WebformSubmissionStorageInterface::PURGE_ALL, [ [TRUE, TRUE], [TRUE, TRUE], diff --git a/webform.module b/webform.module index a810eff9..8d95fcff 100644 --- a/webform.module +++ b/webform.module @@ -106,9 +106,8 @@ function webform_modules_uninstalled($modules) { * Implements hook_cron(). */ function webform_cron() { - $purge = \Drupal::service('webform.purge'); $config = \Drupal::config('webform.settings'); - $purge->purgeWebformSubmission($config->get('purge_settings.cron_size')); + \Drupal::entityTypeManager()->getStorage('webform_submission')->purgeWebformSubmission($config->get('purge_settings.cron_size')); } /** diff --git a/webform.services.yml b/webform.services.yml index da35441f..6088a57d 100644 --- a/webform.services.yml +++ b/webform.services.yml @@ -68,10 +68,6 @@ services: class: Drupal\webform\WebformTokenManager arguments: ['@module_handler', '@token'] - webform.purge: - class: Drupal\webform\WebformSubmissionPurge - arguments: ['@entity.query', '@entity_type.manager'] - # Subscribers. webform.route_subscriber: