Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OS-64 Setting a standard value for Automatic purge #80

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [OS-64] Setting a standard value for Automatic purge

## [3.13.3] 2023-12-05

- [#76](https://github.com/OS2Forms/os2forms/pull/76)
Expand Down
33 changes: 30 additions & 3 deletions modules/os2forms_forloeb/os2forms_forloeb.module
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ function _os2forms_forloeb_end_notification_batch_function($processID, $queueID)
function os2forms_forloeb_webform_create(WebformInterface $webform) {
// Set purge of all users submissions.
$webform->setSetting('purge', 'all');
// Set purge of submissions more than 30 days old.

// Set purge of submissions if empty.
if (empty($webform->getSetting('purge_days'))) {
$webform->setSetting('purge_days', '30');
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$os2forms_forloeb_settings = $third_party_settings_manager->getThirdPartySetting('os2forms', 'os2forms_forloeb') ?: 30;

$webform->setSetting('purge_days', $os2forms_forloeb_settings['purge_days']);
}
}

Expand All @@ -158,7 +163,11 @@ function os2forms_forloeb_webform_create(WebformInterface $webform) {
function os2forms_forloeb_webform_presave(WebformInterface $webform) {
// Add a purge time frame if not set.
if (empty($webform->getSetting('purge_days'))) {
$webform->setSetting('purge_days', 30);
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$os2forms_forloeb_settings = $third_party_settings_manager->getThirdPartySetting('os2forms', 'os2forms_forloeb') ?: 30;

$webform->setSetting('purge_days', $os2forms_forloeb_settings['purge_days']);
}
}

Expand Down Expand Up @@ -374,3 +383,21 @@ function os2forms_forloeb_theme(array &$variables) {
function _os2forms_forloeb_helper(): MaestroHelper {
return Drupal::service(MaestroHelper::class);
}

/**
* Implements hook_form_FORM_ID_alter().
*
* Exposing settings for general OS2forms admin settings.
*/
function os2forms_forloeb_form_os2forms_settings_alter(&$form, FormStateInterface $form_state) {
/** @var \Drupal\webform\WebformThirdPartySettingsManagerInterface $third_party_settings_manager */
$third_party_settings_manager = \Drupal::service('webform.third_party_settings_manager');
$os2forms_forloeb_settings = $third_party_settings_manager->getThirdPartySetting('os2forms', 'os2forms_forloeb') ?: 30;

$form['third_party_settings']['os2forms']['os2forms_forloeb']['purge_days'] = [
'#type' => 'textfield',
'#title' => t('Default number of days to retain submissions'),
'#default_value' => !(empty($os2forms_forloeb_settings)) ? $os2forms_forloeb_settings['purge_days'] : 30,
'#description' => t('Default value is used when creating a new form, after that it is saved on a form level'),
];
}
4 changes: 4 additions & 0 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
foreach ($settings as $settingKey => $settingValues) {
$savedSettings = $this->thirdPartySettingsManager->getThirdPartySetting($module_key, $settingKey);
if (is_array($settingValues)) {
if (!$savedSettings) {
$savedSettings = [];
}

$savedSettings = array_replace($savedSettings, $settingValues);
}
else {
Expand Down