Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit b5a2804

Browse files
authored
Merge pull request #141 from OS2Forms/develop
Deploying 2.5.0
2 parents 05fedb4 + 17d7d05 commit b5a2804

File tree

7 files changed

+172
-35
lines changed

7 files changed

+172
-35
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Ignore IntelliJ
2-
.idea/*
2+
.idea/*
3+
4+
composer.lock
5+
vendor

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ before starting to add changes.
99

1010
## [Unreleased]
1111

12+
## 2.5.0 - 11.10.2022
13+
14+
### Added
15+
- retry task controller action
16+
- Added support for inheriting values without creating a submission
17+
18+
## 2.4.0
19+
1220
### Added
1321
- Github CI action for checking Drupal Coding standards with PHP Code Sniffer
1422
- Fixed coding standards issues

composer.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@
7979
},
8080
"drupal/coc_forms_auto_export": {
8181
"3240592 - Problem with phpseclib requirement in 2.x (https://www.drupal.org/project/coc_forms_auto_export/issues/3240592)": "https://www.drupal.org/files/issues/2021-10-04/requirement-namespace-3240592-1.patch"
82-
},
83-
"drupal/smtp": {
84-
"Duplicated attachments in emails generated by Webform: (https://www.drupal.org/project/smtp/issues/2995290)": "https://www.drupal.org/files/issues/2021-11-02/smtp-2995290-23.patch"
8582
}
8683
}
8784
},
@@ -100,6 +97,12 @@
10097
]
10198
},
10299
"config": {
103-
"sort-packages": true
100+
"sort-packages": true,
101+
"allow-plugins": {
102+
"simplesamlphp/composer-module-installer": true,
103+
"dealerdirect/phpcodesniffer-composer-installer": true,
104+
"cweagans/composer-patches": true,
105+
"zaporylie/composer-drupal-optimizations": true
106+
}
104107
}
105108
}

os2forms_forloeb.module

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ use Drupal\Core\Form\FormStateInterface;
1313
use Drupal\Core\Render\BubbleableMetadata;
1414
use Drupal\Core\Url;
1515
use Drupal\maestro\Engine\MaestroEngine;
16+
use Drupal\os2forms_forloeb\Plugin\EngineTasks\MaestroWebformInheritTask;
1617
use Drupal\webform\Entity\WebformSubmission;
1718
use Drupal\user\Entity\User;
19+
use Drupal\webform\WebformSubmissionInterface;
1820

1921
/**
2022
* Implements hook_maestro_interactive_handlers().
@@ -96,7 +98,7 @@ function os2forms_forloeb_workflow_maestro_reassign_form_submit(&$form, &$form_s
9698
*/
9799
function os2forms_forloeb_maestro_batch_handlers() {
98100
return [
99-
'end_notification_batch_function' => t('Batch function to send out flow completion notification to initiator.'),
101+
'_os2forms_forloeb_end_notification_batch_function' => t('Batch function to send out flow completion notification to initiator.'),
100102
];
101103
}
102104

@@ -108,7 +110,7 @@ function os2forms_forloeb_maestro_batch_handlers() {
108110
* @param int $queueID
109111
* The Maestro queue ID.
110112
*/
111-
function end_notification_batch_function($processID, $queueID) {
113+
function _os2forms_forloeb_end_notification_batch_function($processID, $queueID) {
112114

113115
/*
114116
* Pseudocode for handling this:
@@ -343,3 +345,10 @@ function os2forms_forloeb_maestro_post_fetch_assigned_queue_tasks($userID, &$que
343345
$queueIDs = array_unique($queueIDs);
344346
}
345347
}
348+
349+
/**
350+
* Implements hook_ENTITY_TYPE_prepare_form().
351+
*/
352+
function os2forms_forloeb_webform_submission_prepare_form(WebformSubmissionInterface $webform_submission, string $operation, FormStateInterface $form_state) {
353+
MaestroWebformInheritTask::webformSubmissionPrepareForm($webform_submission, $operation, $form_state);
354+
}

os2forms_forloeb.routing.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ os2forms_forloeb.forloeb_task_console_controller_execute:
77
_permission: 'access content'
88
options:
99
no_cache: TRUE
10+
11+
os2forms_forloeb.forloeb_task_console_controller_execute_retry:
12+
path: 'os2forms-forloeb/execute-task-retry'
13+
defaults:
14+
_controller: '\Drupal\os2forms_forloeb\Controller\ForloebTaskConsoleController::retry'
15+
_title: 'Task not yet ready'
16+
requirements:
17+
_permission: 'access content'
18+
options:
19+
no_cache: TRUE

src/Controller/ForloebTaskConsoleController.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Component\Utility\UrlHelper;
66
use Drupal\Core\Controller\ControllerBase;
77
use Drupal\Core\Entity\EntityTypeManagerInterface;
8+
use Drupal\Core\StringTranslation\StringTranslationTrait;
89
use Drupal\Core\Url;
910
use Drupal\maestro\Engine\MaestroEngine;
1011
use Drupal\maestro\Utility\TaskHandler;
@@ -19,6 +20,8 @@
1920
*/
2021
class ForloebTaskConsoleController extends ControllerBase {
2122

23+
use StringTranslationTrait;
24+
2225
/**
2326
* Update manager service.
2427
*
@@ -82,6 +85,12 @@ public function execute() {
8285
$token = $this->requestStack->getCurrentRequest()->query->get('os2forms-forloeb-ws-token', '');
8386
if ($token) {
8487
$queueRecord = $this->forloebTaskConsole->getQueueIdByWebformSubmissionToken($token);
88+
if (empty($queueRecord)) {
89+
return new RedirectResponse(
90+
Url::fromRoute('os2forms_forloeb.forloeb_task_console_controller_execute_retry',
91+
['referrer' => \Drupal::request()->getRequestUri()])->toString()
92+
);
93+
}
8594
}
8695
else {
8796
// For empty token there is user last task from taskconsole queue.
@@ -150,4 +159,18 @@ public function execute() {
150159
return new RedirectResponse($redirect_to->toString());
151160
}
152161

162+
/**
163+
* Show message about task not yet ready.
164+
*
165+
* @return array
166+
* The render array.
167+
*/
168+
public function retry() {
169+
$referrer = $this->requestStack->getCurrentRequest()->query->get('referrer', '#');
170+
171+
return [
172+
'#markup' => $this->t('Your task is not yet ready. Please <a href=":referrer">try again</a> in 5 minutes.', [':referrer' => $referrer]),
173+
];
174+
}
175+
153176
}

src/Plugin/EngineTasks/MaestroWebformInheritTask.php

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\maestro\Form\MaestroExecuteInteractive;
1010
use Drupal\maestro\Engine\MaestroEngine;
1111
use Drupal\Core\Form\FormStateInterface;
12+
use Drupal\webform\WebformSubmissionInterface;
1213
use Symfony\Component\HttpFoundation\RedirectResponse;
1314

1415
/**
@@ -74,6 +75,12 @@ public function getTaskEditForm(array $task, $templateMachineName) {
7475
'#default_value' => $task['data']['inherit_webform_unique_id'] ?? '',
7576
'#required' => TRUE,
7677
];
78+
$form['inherit_webform_create_submission'] = [
79+
'#type' => 'checkbox',
80+
'#title' => $this->t('Create submission'),
81+
'#description' => $this->t('Create submission'),
82+
'#default_value' => $task['data']['inherit_webform_create_submission'] ?? FALSE,
83+
];
7784
return $form;
7885
}
7986

@@ -86,6 +93,7 @@ public function prepareTaskForSave(array &$form, FormStateInterface $form_state,
8693
parent::prepareTaskForSave($form, $form_state, $task);
8794
// Add custom field(s) to the inherited prepareTaskForSave method.
8895
$task['data']['inherit_webform_unique_id'] = $form_state->getValue('inherit_webform_unique_id');
96+
$task['data']['inherit_webform_create_submission'] = $form_state->getValue('inherit_webform_create_submission');
8997
}
9098

9199
/**
@@ -120,48 +128,121 @@ public function getExecutableForm($modal, MaestroExecuteInteractive $parent) {
120128
}
121129
// Now create webform submission, submit and attach to current process.
122130
$templateTask = MaestroEngine::getTemplateTaskByQueueID($this->queueID);
123-
$taskUniqueSubmissionId = $templateTask['data']['unique_id'];
124131
$webformMachineName = $templateTask['data']['webform_machine_name'];
125132

126133
$values = [];
127134
$values['webform_id'] = $webformMachineName;
128135
$values['data'] = $field_values;
129136

130-
// Create submission.
131-
$new_submission = WebformSubmission::create($values);
137+
$createSubmission = (bool) ($task['data']['inherit_webform_create_submission'] ?? FALSE);
138+
if ($createSubmission) {
139+
// Create submission.
140+
$new_submission = WebformSubmission::create($values);
141+
142+
// Submit the webform submission.
143+
$submission = WebformSubmissionForm::submitWebformSubmission($new_submission);
144+
145+
// WebformSubmissionForm::submitWebformSubmission returns an array
146+
// if the submission is not valid.
147+
if (is_array($submission)) {
148+
\Drupal::logger('os2forms_forloeb')->error(
149+
"Can't create new submission: " . json_encode($submission)
150+
);
151+
\Drupal::messenger()->addError('Webform data is invalid and could not be submitted.');
152+
return FALSE;
153+
}
132154

133-
// Submit the webform submission.
134-
$submission = WebformSubmissionForm::submitWebformSubmission($new_submission);
155+
$taskUniqueSubmissionId = $templateTask['data']['unique_id'];
135156

136-
// WebformSubmissionForm::submitWebformSubmission returns an array
137-
// if the submission is not valid.
138-
if (is_array($submission)) {
139-
\Drupal::logger('os2forms_forloeb')->error(
140-
"Can't create new submission: " . json_encode($submission)
157+
// Attach it to the Maestro process.
158+
$sid = $new_submission->id();
159+
MaestroEngine::createEntityIdentifier(
160+
$this->processID, $new_submission->getEntityTypeId(),
161+
$new_submission->bundle(), $taskUniqueSubmissionId, $sid
141162
);
142-
\Drupal::messenger()->addError('Webform data is invalid and could not be submitted.');
143-
return FALSE;
144-
}
145163

146-
// Attach it to the Maestro process.
147-
$sid = $new_submission->id();
148-
MaestroEngine::createEntityIdentifier(
149-
$this->processID, $new_submission->getEntityTypeId(),
150-
$new_submission->bundle(), $taskUniqueSubmissionId, $sid
151-
);
152-
153-
$form = parent::getExecutableForm($modal, $parent);
154-
// Catch os2forms-forloeb access token and pass it further.
155-
if ($form instanceof RedirectResponse && $token = \Drupal::request()->query->get('os2forms-forloeb-ws-token')) {
156-
// Check token to previous submission and update it to new one.
157-
if ($token === $webform_submission->getToken()) {
158-
$token = $new_submission->getToken();
159-
$url = Url::fromUserInput($form->getTargetUrl(), ['query' => ['os2forms-forloeb-ws-token' => $token]]);
160-
$form = new RedirectResponse($url->toString());
164+
// Important: Apparently the form must be generated after calling
165+
// MaestroEngine::createEntityIdentifier for this to work.
166+
$form = parent::getExecutableForm($modal, $parent);
167+
// Catch os2forms-forloeb access token and pass it further.
168+
if ($form instanceof RedirectResponse && $token = \Drupal::request()->query->get('os2forms-forloeb-ws-token')) {
169+
// Check token to previous submission and update it to new one.
170+
if ($token === $webform_submission->getToken()) {
171+
$token = $new_submission->getToken();
172+
$url = Url::fromUserInput($form->getTargetUrl(), ['query' => ['os2forms-forloeb-ws-token' => $token]]);
173+
$form = new RedirectResponse($url->toString());
174+
}
161175
}
162176
}
177+
else {
178+
// Store values in session.
179+
$values['processID'] = $this->processID;
180+
$values['queueID'] = $this->queueID;
181+
$values['webformInheritID'] = $webformInheritID;
182+
183+
self::setTaskValues($this->queueID, $values);
184+
185+
$form = parent::getExecutableForm($modal, $parent);
186+
}
163187

164188
return $form;
165189
}
166190

191+
/**
192+
* Implements hook_ENTITY_TYPE_prepare_form().
193+
*/
194+
public static function webformSubmissionPrepareForm(WebformSubmissionInterface $webformSubmission, string $operation, FormStateInterface $formState): void {
195+
$request = \Drupal::request();
196+
$isMaestro = (bool) $request->query->get('maestro', 0);
197+
$queueID = (int) $request->query->get('queueid', 0);
198+
if ($isMaestro && $queueID > 0) {
199+
$values = self::getTaskValues($queueID);
200+
if (isset($values['data'])) {
201+
foreach ($values['data'] as $name => $value) {
202+
$webformSubmission->setElementData($name, $value);
203+
}
204+
}
205+
}
206+
}
207+
208+
/**
209+
* Get task values from session.
210+
*
211+
* @param int $queueID
212+
* The queue ID.
213+
*
214+
* @return array
215+
* The task values if any.
216+
*/
217+
private static function getTaskValues($queueID) {
218+
$sessionKey = self::formatTaskValuesSessionKey($queueID);
219+
return \Drupal::request()->getSession()->get($sessionKey);
220+
}
221+
222+
/**
223+
* Set task values in session.
224+
*
225+
* @param int $queueID
226+
* The queue ID.
227+
* @param array $values
228+
* The values.
229+
*/
230+
private static function setTaskValues($queueID, array $values) {
231+
$sessionKey = self::formatTaskValuesSessionKey($queueID);
232+
\Drupal::request()->getSession()->set($sessionKey, $values);
233+
}
234+
235+
/**
236+
* Format task values session key.
237+
*
238+
* @param int $queueID
239+
* The queue ID.
240+
*
241+
* @return string
242+
* The formatted session key.
243+
*/
244+
private static function formatTaskValuesSessionKey($queueID) {
245+
return sprintf('os2forms_forloeb_inherited_values_%s', $queueID);
246+
}
247+
167248
}

0 commit comments

Comments
 (0)