Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ before starting to add changes. Use example [placed in the end of the page](#exa

## [Unreleased]

- [PR-333](https://github.com/OS2Forms/os2forms/pull/333)
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
- Moved digital signature configuration (which attachment to sign and the
signature validation text position) from the *OS2Forms Attachment* element
onto the *Digital Signature* webform handler. Includes an automatic
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "EUPL-1.2",
"type": "drupal-module",
"require": {
"php": "^8.1",
"php": "^8.3",
"ext-dom": "*",
"ext-soap": "*",
"cweagans/composer-patches": "^1.6.5",
Expand Down
32 changes: 32 additions & 0 deletions modules/os2forms_digital_post/os2forms_digital_post.install
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\os2forms_digital_post\Helper\BeskedfordelerHelper;
use Drupal\webform\WebformSubmissionInterface;

/**
* Implements hook_schema().
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Comment thread
jekuaitk marked this conversation as resolved.
Resolved

/**
* Maximum length of sender label.
Expand Down Expand Up @@ -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],
],
];
}

Expand Down Expand Up @@ -203,6 +208,30 @@ 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('<b>draft is created</b>.'),
WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this->t('<b>draft is updated</b>.'),
WebformSubmissionInterface::STATE_CONVERTED => $this->t('anonymous <b>submission is converted</b> to authenticated.'),
WebformSubmissionInterface::STATE_COMPLETED => $this->t('<b>submission is completed</b>.'),
WebformSubmissionInterface::STATE_UPDATED => $this->t('<b>submission is updated</b>.'),
WebformSubmissionInterface::STATE_LOCKED => $this->t('<b>submission is locked</b>.'),
],
'#access' => !$resultsDisabled,
'#default_value' => $resultsDisabled ? [WebformSubmissionInterface::STATE_COMPLETED] : $states,
];

return $this->setSettingsParents($form);
}

Expand Down Expand Up @@ -333,6 +362,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;
}

/**
Expand All @@ -341,6 +375,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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,15 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function preSave(WebformSubmissionInterface $webform_submission) {
$webform = $webform_submission->getWebform();

if ($webform_submission->isLocked()) {
// Signing redirects the user to the signing flow, so it only makes sense
// on an actual submission. Drafts are skipped, and a locked submission is
// already signed (signing locks it).
if ($webform_submission->isDraft() || $webform_submission->isLocked()) {
return;
}

$webform = $webform_submission->getWebform();

$attachment = $this->getSubmissionAttachment($webform_submission);
if (!$attachment) {
$this->logger->error('Attachment cannot be created webform: %webform, webform_submission: %webform_submission',
Expand Down
33 changes: 33 additions & 0 deletions modules/os2forms_fasit/os2forms_fasit.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Install hooks for os2forms_fasit.
*/

use Drupal\webform\WebformSubmissionInterface;

/**
* Install Key module.
*/
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typed class constants require PHP 8.3, while this package still declares "php": "^8.1". On PHP 8.1/8.2, loading any of these three handlers will cause a parse error, breaking forms that use them.

We either need to remove string type here or bump the PHP version in composer.json

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added php 8.3 requirement


/**
* The submission logger.
Expand Down Expand Up @@ -79,6 +81,19 @@ public static function create(ContainerInterface $container, array $configuratio
);
}

/**
* {@inheritdoc}
*
* @phpstan-return array<string, mixed>
*/
public function defaultConfiguration() {
return [
self::ADDITIONAL => [
self::STATES => [WebformSubmissionInterface::STATE_COMPLETED],
],
];
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -129,6 +144,30 @@ 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('<b>draft is created</b>.'),
WebformSubmissionInterface::STATE_DRAFT_UPDATED => $this->t('<b>draft is updated</b>.'),
WebformSubmissionInterface::STATE_CONVERTED => $this->t('anonymous <b>submission is converted</b> to authenticated.'),
WebformSubmissionInterface::STATE_COMPLETED => $this->t('<b>submission is completed</b>.'),
WebformSubmissionInterface::STATE_UPDATED => $this->t('<b>submission is updated</b>.'),
WebformSubmissionInterface::STATE_LOCKED => $this->t('<b>submission is locked</b>.'),
],
'#access' => !$resultsDisabled,
'#default_value' => $resultsDisabled ? [WebformSubmissionInterface::STATE_COMPLETED] : $states,
];

return $this->setSettingsParents($form);
}

Expand All @@ -143,12 +182,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');
Expand Down
39 changes: 39 additions & 0 deletions modules/os2forms_fbs_handler/os2forms_fbs_handler.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @file
* Install hooks for os2forms_fbs_handler.
*/

use Drupal\webform\WebformSubmissionInterface;

/**
* Set states config to completed on existing fbs handlers.
*/
function os2forms_fbs_handler_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_fbs') {
continue;
}
$handlers[$handlerKey]['settings']['additional']['states'] = [WebformSubmissionInterface::STATE_COMPLETED];
$changed = TRUE;
}

if ($changed) {
$config->set('handlers', $handlers)->save();
}
}
}
Loading
Loading