Skip to content

Commit 123d488

Browse files
authored
Merge pull request #187 from OS2Forms/feature/avoid-double-save-on-name-and-address-protected-submission
#186: Avoid multiple saves when handling name and address protection
2 parents 2fb181d + 08a7b53 commit 123d488

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1111

1212
## [Unreleased]
1313

14+
- Avoid double-saving submissions when handling name and address protection.
1415
- [PR-191](https://github.com/OS2Forms/os2forms/pull/191)
1516
Re-throws exception to ensure failed status during Maestro notification job.
1617
- [PR-202](https://github.com/OS2Forms/os2forms/pull/202)

modules/os2forms_nemid/os2forms_nemid.module

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,5 +469,4 @@ function os2forms_nemid_submission_set_address_protected(array $form, FormStateI
469469
$data = $webformSubmission->getData();
470470
$data['os2forms_nemid_elements_nemid_address_protected'] = TRUE;
471471
$webformSubmission->setData($data);
472-
$webformSubmission->save();
473472
}

modules/os2forms_nemid/src/Plugin/WebformElement/NemidAddress.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,19 @@ public function alterForm(array &$element, array &$form, FormStateInterface $for
4747
// Only manipulate element on submission create form.
4848
if (!$webformSubmission->isCompleted()) {
4949
if ($cprLookupResult && $cprLookupResult->isNameAddressProtected()) {
50-
$element['#info_message'] = 'adresse beskyttelse';
5150
NestedArray::setValue($form['elements'], $element['#webform_parents'], $element);
52-
$form['actions']['submit']['#submit'][] = 'os2forms_nemid_submission_set_address_protected';
51+
52+
// It is important the 'os2forms_nemid_submission_set_address_protected'
53+
// submit action is executed before the 'save' action. Otherwise,
54+
// submissions are both created and completed, resulting in unexpected
55+
// behavior, e.g. handlers being run twice.
56+
if (isset($form['actions']['submit']['#submit']) && is_array($form['actions']['submit']['#submit'])) {
57+
array_unshift($form['actions']['submit']['#submit'], 'os2forms_nemid_submission_set_address_protected');
58+
}
59+
else {
60+
$form['actions']['submit']['#submit'][] = 'os2forms_nemid_submission_set_address_protected';
61+
}
62+
5363
}
5464
}
5565
else {

0 commit comments

Comments
 (0)