Skip to content

Commit 99d35a8

Browse files
committed
More stuff
1 parent 10403c2 commit 99d35a8

File tree

4 files changed

+156
-28
lines changed

4 files changed

+156
-28
lines changed

web/modules/custom/os2forms_maestro_webform/src/MaestroHelper.php

+34-26
Original file line numberDiff line numberDiff line change
@@ -58,53 +58,61 @@ public function maestroZeroUserNotification($templateMachineName, $taskMachineNa
5858
$templateTask = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskMachineName);
5959
$taskType = $templateTask['tasktype'] ?? NULL;
6060
if (in_array($taskType, ['MaestroWebform', 'MaestroWebformInherit'], TRUE)) {
61-
if ($processID = MaestroEngine::getProcessIdFromQueueId($queueID) ?: NULL) {
62-
$entityIdentifiers = MaestroEngine::getAllEntityIdentifiersForProcess($processID);
63-
foreach ($entityIdentifiers as $entityIdentifier) {
64-
if ('webform_submission' === ($entityIdentifier['entity_type'] ?? NULL)) {
65-
$submission = $this->webformSubmissionStorage->load($entityIdentifier['entity_id']);
66-
if ($submission) {
67-
$this->handleSubmissionNotification($submission, $templateTask, $queueID);
68-
}
61+
if ($processID = (MaestroEngine::getProcessIdFromQueueId($queueID) ?: NULL)) {
62+
// Get webforn submissions in process.
63+
$entityIdentifiers = self::getWebformSubmissionIdentifiersForProcess($processID);
64+
65+
// Process only the latest entity identifier.
66+
if ($entityIdentifier = reset($entityIdentifiers)) {
67+
$submission = $this->webformSubmissionStorage->load($entityIdentifier['entity_id']);
68+
if ($submission) {
69+
$this->handleSubmissionNotification($submission, $templateTask, $queueID);
6970
}
7071
}
7172
}
7273
}
7374
}
7475
}
7576

77+
/**
78+
* Get webform submission identifiers for a process.
79+
*
80+
* @param int $processID
81+
* The Maestro Process ID.
82+
*
83+
* @return array
84+
* The webform submission identifiers sorted ascendingly by creation time.
85+
*/
86+
public static function getWebformSubmissionIdentifiersForProcess(int $processID): array {
87+
// Get webform submissions in process.
88+
$entityIdentifiers = array_filter(
89+
MaestroEngine::getAllEntityIdentifiersForProcess($processID),
90+
static fn (array $entityIdentifier) => 'webform_submission' === ($entityIdentifier['entity_type'] ?? NULL)
91+
);
92+
93+
// Sort by entity ID.
94+
uasort($entityIdentifiers, static fn (array $a, array $b) => ($b['entity_id'] ?? 0) <=> ($a['entity_id'] ?? 0));
95+
96+
return $entityIdentifiers;
97+
}
98+
7699
/**
77100
* Implements hook_maestro_can_user_execute_task_alter().
78101
*/
79102
public function maestroCanUserExecuteTaskAlter(bool &$returnValue, int $queueID, int $userID): void {
80-
81-
// Check if this is an anonymous user and we've been barred access already.
82-
if ($userID == 0 && $returnValue === FALSE) {
83-
// Load the template task and we'll determine if this has our "special"
84-
// assignment to a known "anonymous" role.
103+
// Perform our checks only if an anonymous user has been barred access.
104+
if (0 === $userID && FALSE === $returnValue) {
85105
$templateTask = MaestroEngine::getTemplateTaskByQueueID($queueID);
86106
$assignments = explode(',', $templateTask['assigned']);
87107

108+
// Check if one of the assignments match our known anonymous roles.
88109
$knownAnonymousAssignments = array_map(
89110
static fn(string $role) => 'role:fixed:' . $role,
90111
array_filter($this->config->get('known_anonymous_roles') ?: [])
91112
);
92113

93-
// DEV NOTE!!!! We do NOTHING to ensure that this is a specific task type
94-
// or even that this is in our desired workflows. This routine will run
95-
// for each and every task execution test and only if we're anonymous.
96-
// This should be streamlined and tightened up to check for more specific
97-
// task types or process types... perhaps...
98-
// In our very specific use case, we are assigning to a fixed role of
99-
// Citizen.
100-
// This could be a task config option to denote that regardless of what's
101-
// in the assignment, we validate this task as executable one way or
102-
// another.
103-
// @todo Add in your own validation routines
104114
foreach ($assignments as $assignment) {
105115
if (in_array($assignment, $knownAnonymousAssignments, TRUE)) {
106-
// This is our use case. Very rigid for now for prototyping/demo
107-
// purposes.
108116
$returnValue = TRUE;
109117
}
110118
}

web/modules/custom/os2forms_maestro_webform/src/Plugin/WebformHandler/NotificationHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function create(ContainerInterface $container, array $configuratio
109109
*/
110110
public function getSummary() {
111111
return [
112-
'#markup' => $this->t('<strong>Note</strong> This a not a real webform handler run when a submission is created, but run when Maestro sends out a notification. The notification will be sent to the person identified by the value of the %element element.', [
112+
'#markup' => $this->t('Sends notification when triggered by Maestro. The notification will be sent to the person identified by the value of the %element element.', [
113113
'%element' => $this->configuration[self::NOTIFICATION][self::RECIPIENT_ELEMENT] ?? NULL,
114114
]),
115115
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_maestro_webform\Plugin\WebformHandler;
4+
5+
use Drupal\Core\Form\FormStateInterface;
6+
use Drupal\Core\Serialization\Yaml;
7+
use Drupal\maestro\Engine\MaestroEngine;
8+
use Drupal\os2forms_digital_post\Helper\WebformHelperSF1601;
9+
use Drupal\os2forms_maestro_webform\MaestroHelper;
10+
use Drupal\webform\Plugin\WebformHandlerBase;
11+
use Drupal\webform\WebformInterface;
12+
use Symfony\Component\DependencyInjection\ContainerInterface;
13+
14+
/**
15+
* Maestro notification handler.
16+
*
17+
* @WebformHandler(
18+
* id = "os2forms_maestro_webform_workflow_submission_element",
19+
* label = @Translation("Maestro workflow submission element"),
20+
* category = @Translation("Web services"),
21+
* description = @Translation("Prefills form elements with values from workflow submissions."),
22+
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
23+
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_IGNORED,
24+
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
25+
* )
26+
*/
27+
final class WorkflowSubmissionElementValues extends WebformHandlerBase {
28+
public const SPEC = 'spec';
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
34+
$instance = new static($configuration, $plugin_id, $plugin_definition);
35+
36+
$instance->loggerFactory = $container->get('logger.factory');
37+
$instance->configFactory = $container->get('config.factory');
38+
$instance->renderer = $container->get('renderer');
39+
$instance->entityTypeManager = $container->get('entity_type.manager');
40+
$instance->conditionsValidator = $container->get('webform_submission.conditions_validator');
41+
$instance->tokenManager = $container->get('webform.token_manager');
42+
43+
$instance->setConfiguration($configuration);
44+
45+
return $instance;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function alterElements(array &$elements, WebformInterface $webform) {
52+
$spec = $this->getSpec();
53+
54+
$submissionValues = [];
55+
if ($queueID = \Drupal::request()->query->get('queueid')) {
56+
if ($processID = (MaestroEngine::getProcessIdFromQueueId($queueID) ?: NULL)) {
57+
$entityIdentifiers = MaestroHelper::getWebformSubmissionIdentifiersForProcess($processID);
58+
foreach ($entityIdentifiers as $entityIdentifier) {
59+
/** @var \Drupal\webform\WebformSubmissionInterface $submission */
60+
$submission = $this->submissionStorage->load($entityIdentifier['entity_id']);
61+
$webform = $submission->getWebform();
62+
$submissionValues[$webform->id()] = $submission->getData();
63+
}
64+
}
65+
}
66+
67+
foreach ($spec as $key => $info) {
68+
if (isset($elements[$key])) {
69+
if (isset($info['form'], $info['element'])
70+
&& isset($submissionValues[$info['form']])
71+
&& isset($submissionValues[$info['form']][$info['element']])) {
72+
$elements[$key]['#default_value'] = $submissionValues[$info['form']][$info['element']];
73+
}
74+
}
75+
}
76+
}
77+
78+
private function getSpec(): array {
79+
$spec = [];
80+
try {
81+
$spec = Yaml::decode($this->configuration[self::SPEC]);
82+
} catch (\Throwable) {}
83+
84+
return is_array($spec) ? $spec : [];
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function getSummary() {
91+
return [
92+
'#markup' => $this->t('Fill elements with flow values: <pre>%spec</pre>', ['%spec' => Yaml::encode($this->getSpec())]),
93+
];
94+
}
95+
96+
/**
97+
* {@inheritdoc}
98+
*/
99+
public function buildConfigurationForm(array $form, FormStateInterface $formState) {
100+
$form[self::SPEC] = [
101+
'#type' => 'textarea',
102+
'#title' => $this->t('Spec'),
103+
'#description' => $this->t('Default value on elements will override this'),
104+
'#required' => TRUE,
105+
'#default_value' => $this->configuration[self::SPEC] ?? NULL,
106+
];
107+
108+
return $this->setSettingsParents($form);
109+
}
110+
111+
/**
112+
* {@inheritdoc}
113+
*/
114+
public function submitConfigurationForm(array &$form, FormStateInterface $formState) {
115+
parent::submitConfigurationForm($form, $formState);
116+
117+
$this->configuration[self::SPEC] = $formState->getValue(self::SPEC);
118+
}
119+
120+
}

web/modules/custom/os2forms_selvbetjening/os2forms_selvbetjening.module

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function os2forms_selvbetjening_form_alter(array &$form, FormStateInterface $for
2323
function os2forms_selvbetjening_webform_element_alter(array &$element, FormStateInterface $form_state, array $context) {
2424
// Add js-form-item class to webform_more element to expose them to form
2525
// states (element conditions).
26-
if ('webform_more' === $element['#type']) {
26+
if ('webform_more' === ($element['#type'] ?? NULL)) {
2727
$element['#attributes']['class'][] = 'js-form-item';
2828
}
2929
}

0 commit comments

Comments
 (0)