This repository was archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos2forms_forloeb.module
More file actions
354 lines (317 loc) · 11.8 KB
/
os2forms_forloeb.module
File metadata and controls
354 lines (317 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* @file
* Install, update and uninstall functions for the os2forms_forloeb.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\webform\WebformInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Drupal\maestro\Engine\MaestroEngine;
use Drupal\os2forms_forloeb\Plugin\EngineTasks\MaestroWebformInheritTask;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\user\Entity\User;
use Drupal\webform\WebformSubmissionInterface;
/**
* Implements hook_maestro_interactive_handlers().
*
* Handler for the return of an Array
* of function names and help text that will be displayed in the edit task
* form under the handler field.
*/
function os2forms_forloeb_maestro_interactive_handlers() {
return [
'os2forms_forloeb_workflow_maestro_reassign_form' => t('Tilføj sagsbehandler'),
];
}
/**
* The Reassign for Approval form used in maestro workflows.
*
* @param array $form
* The array that contains the form.
* @param object $obj
* References the calling object.
* @param int $queueID
* The queueID from Maestro.
*/
function os2forms_forloeb_workflow_maestro_reassign_form(array &$form, $obj, $queueID = 0) {
$form['reviewer'] = [
'#id' => 'select_assigned_user',
'#type' => 'entity_autocomplete',
'#target_type' => 'user',
'#default_value' => '',
'#selection_settings' => ['include_anonymous' => FALSE],
'#title' => t('Assign for Review to'),
];
$form['queueID'] = [
'#type' => 'hidden',
'#title' => 'the queue ID in the event we need it in later processing',
'#default_value' => $queueID,
'#description' => ('queueID'),
];
// Overriding the default label "complete".
$form['actions']['submit']['#value'] = t('Assign');
return $form;
}
/**
* Submit handler for The Reassign for Approval form.
*
* This is the submit handler for the Reassign for Approval form
* used in maestro workflows.
*/
function os2forms_forloeb_workflow_maestro_reassign_form_submit(&$form, &$form_state) {
$queueID = intval($form_state->getValue('queueID'));
$processID = MaestroEngine::getProcessIdFromQueueId($queueID);
// Who was selected? Load their username, which is the user attribute
// that Maestro assigns tasks by.
$reviewer_uid = $form_state->getValue('reviewer');
// Pass your uid.
$reviewer = User::load($reviewer_uid);
$reviewer_username = $reviewer->getAccountName();
// Add that user to our maestro process variable.
MaestroEngine::setProcessVariable("assigned_to", $reviewer_username, $processID);
// Provide some feedback.
\Drupal::messenger()->addStatus("Content Reassigned");
}
/**
* Implements hook_maestro_batch_handlers().
*
* Array of function names and help text that will be displayed
* in the edit task form under the handler field.
*/
function os2forms_forloeb_maestro_batch_handlers() {
return [
'_os2forms_forloeb_end_notification_batch_function' => t('Batch function to send out flow completion notification to initiator.'),
];
}
/**
* Finishing callback for batch handler.
*
* @param int $processID
* The Maestro process ID.
* @param int $queueID
* The Maestro queue ID.
*/
function _os2forms_forloeb_end_notification_batch_function($processID, $queueID) {
/*
* Pseudocode for handling this:
*
* * Get entity item for webform which spawned this process
* (ID "submission").
* foreach handler in webform: {
* if (email_handler) {
* email_handler.submit();
* }
* }
*/
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, 'submission');
if ($sid) {
$webform_submission = WebformSubmission::load($sid);
$webform = $webform_submission->getWebform();
$handlers = $webform->getHandlers();
foreach ($handlers as $handler) {
if (method_exists($handler, 'getMessage')) {
$message = $handler->getMessage($webform_submission);
$handler->sendMessage($webform_submission, $message);
}
}
}
return TRUE;
}
/**
* Implements hook_ENTITY_TYPE_create().
*
* Sets a global purge setting for all webform submissions to 30 days.
*/
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.
if (empty($webform->getSetting('purge_days'))) {
$webform->setSetting('purge_days', '30');
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*
* Update webform specific submissions purge settings.
*/
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);
}
}
/**
* Set Process Variable (SPV) function.
*
* Uses the webform's unique identifier referenced in the Maestro "webforms"
* process variable to read a value from the webform submission and return it
* to the SPV task to set the process variable in the task.
* The function assumes that the value to be extracted is the SID of a user
* and returns the username instead.
* This seems to be the easiest way to accomplish this.
*
* The "webforms" process variable is set by the Maestro Webforms
* submission handler which sets a "submission:xxx" value
* in the "webforms" process variable, where "xxx" is the unique ID
* of the submission, or is set by the Maestro Webform Task Type's
* "Unique Identifier" setting when editing a Maestro Webform Task.
*
* @param string $uniqueWebformIdentifier
* The webform's "Unique Identifier" as stored
* in the "webforms" process variable.
* @param string $webformFieldMachineName
* The webform field's machine name
* (listed as "KEY" in the webform builder) you wish to pull the value from.
* @param int $queueID
* Provided by the executing SPV task -- the QueueID of the SPV task.
* @param int $processID
* Provided by the executing SPV task -- the ProcessID of the workflow
* running the SPV task.
*/
function os2forms_forloeb_spv_fetch_entity_username($uniqueWebformIdentifier, $webformFieldMachineName, $queueID, $processID) {
$returnValue = 'unset';
// This is the submission we're eventually after.
$sid = FALSE;
$sid = MaestroEngine::getEntityIdentiferByUniqueID($processID, $uniqueWebformIdentifier);
if ($sid) {
$webform_submission = WebformSubmission::load($sid);
}
if ($webform_submission && array_key_exists($webformFieldMachineName, $webform_submission->getData())) {
$returnValue = $webform_submission->getData()[$webformFieldMachineName];
}
// At this point, the submission's value OR 'unset' is in the $returnValue.
if ($returnValue == 'unset' or !$returnValue) {
return $returnValue;
}
$account = User::load($returnValue);
$username = $account->getUsername();
return $username;
}
/**
* Returns array of custom task-types for OS2forms.
*/
function os2forms_forloeb_get_custom_task_types() {
return ['MaestroWebformMultiple', 'MaestroWebformInherit'];
}
/**
* Implements hook_form_alter() for MaestroWebformMultiple task type.
*
* This has been copied from
* maestro/maestro_webform/maestro_webform.module with a minimal but
* necessary change. See https://www.drupal.org/project/maestro/issues/3243510
* When that issue has been fixed, this hook implementation
* can be safely deleted.
*/
function os2forms_forloeb_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$queueID = intval(\Drupal::request()->query->get('queueid', 0));
$isMaestro = intval(\Drupal::request()->query->get('maestro', 0));
// Both these keys need to exist.
if ($isMaestro && $queueID) {
$templateTask = MaestroEngine::getTemplateTaskByQueueID($queueID);
// Get array of custom task-types.
$os2forms_forloeb_custom_task_types = os2forms_forloeb_get_custom_task_types();
// We only care about custom Task-types
// defined in os2forms_forloeb_get_custom_task_types()
if ($templateTask && in_array($templateTask['tasktype'], $os2forms_forloeb_custom_task_types)) {
$storage = $form_state->getStorage();
if ($storage && array_key_exists('form_display', $storage)) {
$thisForm = $storage['form_display']->get('bundle');
$targetEntityType = $storage['form_display']->get('targetEntityType');
if ($isMaestro == 1 &&
$targetEntityType == 'webform_submission' &&
$templateTask['data']['webform_machine_name'] == $thisForm) {
// We now know this is a webform submission.
// We are going to add in our own form fields here.
$form['maestro'] = [
'#tree' => TRUE,
];
$form['maestro']['type'] = [
'#type' => 'hidden',
'#default_value' => $thisForm,
'#required' => TRUE,
];
$form['maestro']['queue_id'] = [
'#type' => 'hidden',
'#default_value' => $queueID,
'#required' => TRUE,
];
$form['maestro']['process_id'] = [
'#type' => 'hidden',
'#default_value' => MaestroEngine::getProcessIdFromQueueId($queueID),
'#required' => TRUE,
];
$form['actions']['submit']['#submit'][] = 'maestro_webform_webform_type_task_submit';
}
}
}
}
}
/**
* Implements hook_preprocess_page().
*/
function os2forms_forloeb_preprocess_page(&$variables) {
$variables['#attached']['library'][] = 'os2forms_forloeb/os2forms_forloeb';
}
/**
* Implements hook_token_info_alter().
*/
function os2forms_forloeb_token_info_alter(&$data) {
$data['tokens']['webform_submission']['os2forms_forloeb_execute_task'] = [
'name' => t('Execute task path for webform submission'),
'description' => t("The token that can be user to get path for webform submission redirect URL."),
'type' => 'webform_submission',
];
}
/**
* Implements hook_tokens().
*
* Provides token value for webform_submission:os2forms_forloeb_execute_task.
*/
function os2forms_forloeb_tokens($type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type === 'webform_submission' && !empty($data['webform_submission']) && isset($tokens['os2forms_forloeb_execute_task'])) {
$replacements[$tokens['os2forms_forloeb_execute_task']] = Url::fromRoute(
'os2forms_forloeb.forloeb_task_console_controller_execute',
['os2forms-forloeb-ws-token' => $data['webform_submission']->getToken()],
['absolute' => TRUE]
)->toString(TRUE)->getGeneratedUrl();
}
return $replacements;
}
/**
* Implements hook_entity_access().
*
* Allows requests with tokens to view the entity.
*/
function os2forms_forloeb_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation == 'update' && $entity instanceof WebformSubmission) {
$token = \Drupal::request()->query->get('os2forms-forloeb-ws-token');
if ($token && $token === $entity->getToken()) {
return AccessResult::allowed();
}
}
return AccessResult::neutral();
}
/**
* Implements hook_maestro_post_fetch_assigned_queue_tasks().
*/
function os2forms_forloeb_maestro_post_fetch_assigned_queue_tasks($userID, &$queueIDs) {
$token = \Drupal::request()->query->get('os2forms-forloeb-ws-token', '');
if ($token) {
$forloebTaskConsole = Drupal::service('os2forms_forloeb.task_console');
$queueRecord = $forloebTaskConsole->getQueueIdByWebformSubmissionToken($token);
$queueIDs[] = $queueRecord->id();
$queueIDs = array_unique($queueIDs);
}
}
/**
* Implements hook_ENTITY_TYPE_prepare_form().
*/
function os2forms_forloeb_webform_submission_prepare_form(WebformSubmissionInterface $webform_submission, string $operation, FormStateInterface $form_state) {
MaestroWebformInheritTask::webformSubmissionPrepareForm($webform_submission, $operation, $form_state);
}