diff --git a/CHANGELOG.md b/CHANGELOG.md index 118a8e55..28b116d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,13 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- Added the ability to configure on which submission states handlers should run. + The default option is to run on the completed state. Changes was made to the + following handlers: + - Digital post + - Fasit + - FBS +- Made submission have state draft during digital signature flow. - [PR-315](https://github.com/OS2Forms/os2forms/pull/315) Added “Display on“ options to Map element diff --git a/modules/os2forms_digital_post/os2forms_digital_post.install b/modules/os2forms_digital_post/os2forms_digital_post.install index 48768e21..cb410b1d 100644 --- a/modules/os2forms_digital_post/os2forms_digital_post.install +++ b/modules/os2forms_digital_post/os2forms_digital_post.install @@ -6,6 +6,7 @@ */ use Drupal\os2forms_digital_post\Helper\BeskedfordelerHelper; +use Drupal\webform\WebformSubmissionInterface; /** * Implements hook_schema(). @@ -26,3 +27,34 @@ function os2forms_digital_post_update_9001(): void { 'os2web_key', ], TRUE); } + +/** + * Set states config to completed on existing digital post handlers. + */ +function os2forms_digital_post_update_10001(): void { + // To avoid having to load full webforms we load and update webform configs. + $configFactory = \Drupal::configFactory(); + + foreach ($configFactory->listAll('webform.webform.') as $name) { + $config = $configFactory->getEditable($name); + $handlers = $config->get('handlers'); + if (!is_array($handlers)) { + continue; + } + + $changed = FALSE; + + foreach ($handlers as $handlerKey => $handler) { + // $handler['id'] is the handler plugin id. + if (($handler['id'] ?? NULL) !== 'digital_post_sf1601') { + continue; + } + $handlers[$handlerKey]['settings']['additional']['states'] = [WebformSubmissionInterface::STATE_COMPLETED]; + $changed = TRUE; + } + + if ($changed) { + $config->set('handlers', $handlers)->save(); + } + } +} diff --git a/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php b/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php index 6f0e06a9..ae633c01 100644 --- a/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php +++ b/modules/os2forms_digital_post/src/Plugin/WebformHandler/WebformHandlerSF1601.php @@ -32,6 +32,8 @@ final class WebformHandlerSF1601 extends WebformHandlerBase { public const RECIPIENT_ELEMENT = 'recipient_element'; public const ATTACHMENT_ELEMENT = 'attachment_element'; public const SENDER_ADDRESS = 'sender_address'; + private const string ADDITIONAL = 'additional'; + private const string STATES = 'states'; /** * Maximum length of sender label. @@ -76,6 +78,9 @@ public static function create(ContainerInterface $container, array $configuratio public function defaultConfiguration() { return [ 'debug' => FALSE, + self::ADDITIONAL => [ + self::STATES => [WebformSubmissionInterface::STATE_COMPLETED], + ], ]; } @@ -203,6 +208,31 @@ public function buildConfigurationForm(array $form, FormStateInterface $formStat '#default_value' => $this->configuration['debug'] ?? NULL, ]; + // Additional. + // Lifted from EmailWebformHandler::buildConfigurationForm(). + $resultsDisabled = (bool) $this->getWebform()->getSetting('results_disabled'); + $form[self::ADDITIONAL] = [ + '#type' => 'fieldset', + '#title' => $this->t('Additional settings'), + ]; + // Settings: States. + $states = (array) ($this->configuration[self::ADDITIONAL][self::STATES] ?? NULL); + $form[self::ADDITIONAL][self::STATES] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Run handler when …'), + '#options' => [ + WebformSubmissionInterface::STATE_DRAFT_CREATED => $this->t('draft is created.'), + WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this->t('draft is updated.'), + WebformSubmissionInterface::STATE_CONVERTED => $this->t('anonymous submission is converted to authenticated.'), + WebformSubmissionInterface::STATE_COMPLETED => $this->t('submission is completed.'), + WebformSubmissionInterface::STATE_UPDATED => $this->t('submission is updated.'), + WebformSubmissionInterface::STATE_DELETED => $this->t('submission is deleted.'), + WebformSubmissionInterface::STATE_LOCKED => $this->t('submission is locked.'), + ], + '#access' => !$resultsDisabled, + '#default_value' => $resultsDisabled ? [WebformSubmissionInterface::STATE_COMPLETED] : $states, + ]; + return $this->setSettingsParents($form); } @@ -333,6 +363,11 @@ static function (array $action) { $this->configuration[self::MEMO_ACTIONS] = $actions; $this->configuration['debug'] = (bool) $formState->getValue('debug'); + + $additional = $formState->getValue(self::ADDITIONAL); + // Clean up states. + $additional[self::STATES] = array_values(array_filter($additional[self::STATES])); + $this->configuration[self::ADDITIONAL] = $additional; } /** @@ -341,6 +376,12 @@ static function (array $action) { * @phpstan-return void */ public function postSave(WebformSubmissionInterface $webformSubmission, $update = TRUE) { + $submissionState = $webformSubmission->getWebform()->getSetting('results_disabled') ? WebformSubmissionInterface::STATE_COMPLETED : $webformSubmission->getState(); + $enabledStates = (array) ($this->configuration[self::ADDITIONAL][self::STATES] ?? NULL); + if (!in_array($submissionState, $enabledStates)) { + return; + } + $this->helper->createJob($webformSubmission, $this->configuration); } diff --git a/modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php b/modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php index 656bf7d4..2f799273 100644 --- a/modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php +++ b/modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php @@ -136,6 +136,8 @@ public function signCallback($uuid, $hash, $fid = NULL) { $this->fileSystem->saveData($signedFileContent, $expectedFileUri, FileExists::Replace); // Updating webform submission. + $this->signingService->setSubmissionCompleted($webformSubmission); + $webformSubmission->setLocked(TRUE); $webformSubmission->save(); diff --git a/modules/os2forms_digital_signature/src/Plugin/WebformHandler/DigitalSignatureWebformHandler.php b/modules/os2forms_digital_signature/src/Plugin/WebformHandler/DigitalSignatureWebformHandler.php index 9a616bfc..26cbdcef 100644 --- a/modules/os2forms_digital_signature/src/Plugin/WebformHandler/DigitalSignatureWebformHandler.php +++ b/modules/os2forms_digital_signature/src/Plugin/WebformHandler/DigitalSignatureWebformHandler.php @@ -115,6 +115,8 @@ public function preSave(WebformSubmissionInterface $webform_submission) { return; } + $this->signingService->setSubmissionDraft($webform_submission); + $attachment = $this->getSubmissionAttachment($webform_submission); if (!$attachment) { $this->logger->error('Attachment cannot be created webform: %webform, webform_submission: %webform_submission', diff --git a/modules/os2forms_digital_signature/src/Service/SigningService.php b/modules/os2forms_digital_signature/src/Service/SigningService.php index 47505243..a9071863 100644 --- a/modules/os2forms_digital_signature/src/Service/SigningService.php +++ b/modules/os2forms_digital_signature/src/Service/SigningService.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_digital_signature\Service; +use Drupal\webform\WebformSubmissionInterface; use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ImmutableConfig; @@ -256,4 +257,34 @@ public function getServiceUrl() : string { return $url; } + /** + * Configure the webform submission as draft. + * + * This is the complementary function to setSubmissionCompleted. + * + * @param \Drupal\webform\WebformSubmissionInterface $webformSubmission + * A webform submission. + * + * @see self::setSubmissionCompleted() + */ + public function setSubmissionDraft(WebformSubmissionInterface $webformSubmission): void { + $webformSubmission->set('in_draft', TRUE); + $webformSubmission->setCompletedTime(0); + } + + /** + * Configure the webform submission as completed. + * + * This is the complementary function to setSubmissionDraft. + * + * @param \Drupal\webform\WebformSubmissionInterface $webformSubmission + * A webform submission. + * + * @see self::setSubmissionDraft() + */ + public function setSubmissionCompleted(WebformSubmissionInterface $webformSubmission): void { + $webformSubmission->set('in_draft', FALSE); + $webformSubmission->setCompletedTime($this->time->getRequestTime()); + } + } diff --git a/modules/os2forms_fasit/os2forms_fasit.install b/modules/os2forms_fasit/os2forms_fasit.install index f862bafb..8d471f49 100644 --- a/modules/os2forms_fasit/os2forms_fasit.install +++ b/modules/os2forms_fasit/os2forms_fasit.install @@ -5,6 +5,8 @@ * Install hooks for os2forms_fasit. */ +use Drupal\webform\WebformSubmissionInterface; + /** * Install Key module. */ @@ -13,3 +15,34 @@ function os2forms_fasit_update_9001(): void { 'key', ], TRUE); } + +/** + * Set states config to completed on existing fasit handlers. + */ +function os2forms_fasit_update_10001(): void { + // To avoid having to load full webforms we load and update webform configs. + $configFactory = \Drupal::configFactory(); + + foreach ($configFactory->listAll('webform.webform.') as $name) { + $config = $configFactory->getEditable($name); + $handlers = $config->get('handlers'); + if (!is_array($handlers)) { + continue; + } + + $changed = FALSE; + + foreach ($handlers as $handlerKey => $handler) { + // $handler['id'] is the handler plugin id. + if (($handler['id'] ?? NULL) !== 'os2forms_fasit') { + continue; + } + $handlers[$handlerKey]['settings']['additional']['states'] = [WebformSubmissionInterface::STATE_COMPLETED]; + $changed = TRUE; + } + + if ($changed) { + $config->set('handlers', $handlers)->save(); + } + } +} diff --git a/modules/os2forms_fasit/src/Plugin/WebformHandler/FasitWebformHandler.php b/modules/os2forms_fasit/src/Plugin/WebformHandler/FasitWebformHandler.php index 84eef379..e726bf44 100644 --- a/modules/os2forms_fasit/src/Plugin/WebformHandler/FasitWebformHandler.php +++ b/modules/os2forms_fasit/src/Plugin/WebformHandler/FasitWebformHandler.php @@ -35,6 +35,8 @@ class FasitWebformHandler extends WebformHandlerBase { public const FASIT_HANDLER_DOCUMENT_DESCRIPTION = 'document_description'; public const FASIT_HANDLER_CPR_ELEMENT = 'cpr_element'; public const FASIT_HANDLER_ATTACHMENT_ELEMENT = 'attachment_element'; + private const string ADDITIONAL = 'additional'; + private const string STATES = 'states'; /** * The submission logger. @@ -79,6 +81,19 @@ public static function create(ContainerInterface $container, array $configuratio ); } + /** + * {@inheritdoc} + * + * @phpstan-return array + */ + public function defaultConfiguration() { + return [ + self::ADDITIONAL => [ + self::STATES => [WebformSubmissionInterface::STATE_COMPLETED], + ], + ]; + } + /** * {@inheritdoc} * @@ -129,6 +144,31 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#size' => 5, ]; + // Additional. + // Lifted from EmailWebformHandler::buildConfigurationForm(). + $resultsDisabled = (bool) $this->getWebform()->getSetting('results_disabled'); + $form[self::ADDITIONAL] = [ + '#type' => 'fieldset', + '#title' => $this->t('Additional settings'), + ]; + // Settings: States. + $states = (array) ($this->configuration[self::ADDITIONAL][self::STATES] ?? NULL); + $form[self::ADDITIONAL][self::STATES] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Run handler when …'), + '#options' => [ + WebformSubmissionInterface::STATE_DRAFT_CREATED => $this->t('draft is created.'), + WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this->t('draft is updated.'), + WebformSubmissionInterface::STATE_CONVERTED => $this->t('anonymous submission is converted to authenticated.'), + WebformSubmissionInterface::STATE_COMPLETED => $this->t('submission is completed.'), + WebformSubmissionInterface::STATE_UPDATED => $this->t('submission is updated.'), + WebformSubmissionInterface::STATE_DELETED => $this->t('submission is deleted.'), + WebformSubmissionInterface::STATE_LOCKED => $this->t('submission is locked.'), + ], + '#access' => !$resultsDisabled, + '#default_value' => $resultsDisabled ? [WebformSubmissionInterface::STATE_COMPLETED] : $states, + ]; + return $this->setSettingsParents($form); } @@ -143,12 +183,23 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s $this->configuration[self::FASIT_HANDLER_GENERAL][self::FASIT_HANDLER_DOCUMENT_DESCRIPTION] = $form_state->getValue(self::FASIT_HANDLER_GENERAL)[self::FASIT_HANDLER_DOCUMENT_DESCRIPTION]; $this->configuration[self::FASIT_HANDLER_GENERAL][self::FASIT_HANDLER_CPR_ELEMENT] = $form_state->getValue(self::FASIT_HANDLER_GENERAL)[self::FASIT_HANDLER_CPR_ELEMENT]; $this->configuration[self::FASIT_HANDLER_GENERAL][self::FASIT_HANDLER_ATTACHMENT_ELEMENT] = $form_state->getValue(self::FASIT_HANDLER_GENERAL)[self::FASIT_HANDLER_ATTACHMENT_ELEMENT]; + + $additional = $form_state->getValue(self::ADDITIONAL); + // Clean up states. + $additional[self::STATES] = array_values(array_filter($additional[self::STATES])); + $this->configuration[self::ADDITIONAL] = $additional; } /** * {@inheritdoc} */ public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE): void { + $submissionState = $webform_submission->getWebform()->getSetting('results_disabled') ? WebformSubmissionInterface::STATE_COMPLETED : $webform_submission->getState(); + $enabledStates = (array) ($this->configuration[self::ADDITIONAL][self::STATES] ?? NULL); + if (!in_array($submissionState, $enabledStates)) { + return; + } + $queueStorage = $this->entityTypeManager->getStorage('advancedqueue_queue'); /** @var \Drupal\advancedqueue\Entity\Queue $queue */ $queue = $queueStorage->load('fasit_queue'); diff --git a/modules/os2forms_fbs_handler/os2forms_fbs_handler.install b/modules/os2forms_fbs_handler/os2forms_fbs_handler.install new file mode 100644 index 00000000..4fa0422f --- /dev/null +++ b/modules/os2forms_fbs_handler/os2forms_fbs_handler.install @@ -0,0 +1,39 @@ +listAll('webform.webform.') as $name) { + $config = $configFactory->getEditable($name); + $handlers = $config->get('handlers'); + if (!is_array($handlers)) { + continue; + } + + $changed = FALSE; + + foreach ($handlers as $handlerKey => $handler) { + // $handler['id'] is the handler plugin id. + if (($handler['id'] ?? NULL) !== 'os2forms_fbs') { + continue; + } + $handlers[$handlerKey]['settings']['additional']['states'] = [WebformSubmissionInterface::STATE_COMPLETED]; + $changed = TRUE; + } + + if ($changed) { + $config->set('handlers', $handlers)->save(); + } + } +} diff --git a/modules/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php b/modules/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php index aa1ae330..33c479c5 100644 --- a/modules/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php +++ b/modules/os2forms_fbs_handler/src/Plugin/WebformHandler/FbsWebformHandler.php @@ -42,6 +42,8 @@ final class FbsWebformHandler extends WebformHandlerBase { * The queue id. */ private const QUEUE_ID = 'os2forms_fbs_handler'; + private const string ADDITIONAL = 'additional'; + private const string STATES = 'states'; /** * Constructs an FbsWebformHandler object. @@ -89,6 +91,19 @@ public static function create(ContainerInterface $container, array $configuratio ); } + /** + * {@inheritdoc} + * + * @phpstan-return array + */ + public function defaultConfiguration() { + return [ + self::ADDITIONAL => [ + self::STATES => [WebformSubmissionInterface::STATE_COMPLETED], + ], + ]; + } + /** * {@inheritdoc} * @@ -146,6 +161,31 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#default_value' => $this->configuration['password'] ?? '', ]; + // Additional. + // Lifted from EmailWebformHandler::buildConfigurationForm(). + $resultsDisabled = (bool) $this->getWebform()->getSetting('results_disabled'); + $form[self::ADDITIONAL] = [ + '#type' => 'fieldset', + '#title' => $this->t('Additional settings'), + ]; + // Settings: States. + $states = (array) ($this->configuration[self::ADDITIONAL][self::STATES] ?? NULL); + $form[self::ADDITIONAL][self::STATES] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Run handler when …'), + '#options' => [ + WebformSubmissionInterface::STATE_DRAFT_CREATED => $this->t('draft is created.'), + WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this->t('draft is updated.'), + WebformSubmissionInterface::STATE_CONVERTED => $this->t('anonymous submission is converted to authenticated.'), + WebformSubmissionInterface::STATE_COMPLETED => $this->t('submission is completed.'), + WebformSubmissionInterface::STATE_UPDATED => $this->t('submission is updated.'), + WebformSubmissionInterface::STATE_DELETED => $this->t('submission is deleted.'), + WebformSubmissionInterface::STATE_LOCKED => $this->t('submission is locked.'), + ], + '#access' => !$resultsDisabled, + '#default_value' => $resultsDisabled ? [WebformSubmissionInterface::STATE_COMPLETED] : $states, + ]; + return $this->setSettingsParents($form); } @@ -164,12 +204,23 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s ->getValue(['wrapper', 'username']); $this->configuration['password'] = $form_state ->getValue(['wrapper', 'password']); + + $additional = $form_state->getValue(self::ADDITIONAL); + // Clean up states. + $additional[self::STATES] = array_values(array_filter($additional[self::STATES])); + $this->configuration[self::ADDITIONAL] = $additional; } /** * {@inheritdoc} */ public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE): void { + $submissionState = $webform_submission->getWebform()->getSetting('results_disabled') ? WebformSubmissionInterface::STATE_COMPLETED : $webform_submission->getState(); + $enabledStates = (array) ($this->configuration[self::ADDITIONAL][self::STATES] ?? NULL); + if (!in_array($submissionState, $enabledStates)) { + return; + } + $logger_context = [ 'handler_id' => 'os2forms_fbs', 'channel' => 'webform_submission',