Skip to content

Commit b554ea8

Browse files
Merge pull request #413 from itk-dev/feature/4239-maestro-task-console-bug
4239: Maestro bycontentfunction function validation
2 parents 226a2cd + d8b0142 commit b554ea8

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.
66

77
## [Under udvikling]
88

9+
* Tilføjede apostrof-regel til kodestandarder [PR-414](https://github.com/itk-dev/os2forms_selvbetjening/pull/414)
10+
* Tilføjede maestro bycontentfunction validation [PR-413](https://github.com/itk-dev/os2forms_selvbetjening/pull/413).
11+
912
## [4.1.0] 2025-04-08
1013

1114
* Ændrede PDF tabel visning [PR-404](https://github.com/itk-dev/os2forms_selvbetjening/pull/404)

config/sync/maestro.settings.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
maestro_send_notifications: 1
2-
maestro_orchestrator_task_console: 1
2+
maestro_orchestrator_task_console: 0
33
maestro_redirect_location: /taskconsole
44
maestro_orchestrator_token: AvfWr5bXdQcMpGTw4NHCMdjY44JXC8w0WOJUnq5s2zA3jh45TRHhCJfxcob0AqS
55
maestro_orchestrator_lock_execution_time: '30'

phpcs.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
</rule>
2222

2323
<rule ref="DrupalPractice"/>
24+
<rule ref="Squiz.Strings.DoubleQuoteUsage.NotRequired"/>
2425
</ruleset>

web/modules/custom/os2forms_email_handler/src/Plugin/WebformHandler/OS2FormsEmailWebformHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private function convertToBytes(string $threshold): int {
214214
$units = ['KB', 'MB', 'GB'];
215215

216216
// Get number of units and units from threshold.
217-
preg_match("/(?<num>\d+)(?<units>kb|mb|gb)$/i", $threshold, $matches);
217+
preg_match('/(?<num>\d+)(?<units>kb|mb|gb)$/i', $threshold, $matches);
218218

219219
$size = (int) $matches['num'];
220220
$unit = strtoupper($matches['units']);
@@ -272,7 +272,7 @@ private function sendFileSizeNotification(WebformSubmissionInterface $webform_su
272272
$notificationMessage['subject'] = $this->t('File size submission warning');
273273

274274
$notificationMessage['body'] = $this->t(
275-
"<p>Dear @name</p><p>Submission @submission attempted sending an email with a large total file size of attachments surpassing @threshold for handler @handler (@handler_id) on form @form (@form_id).</p>", [
275+
'<p>Dear @name</p><p>Submission @submission attempted sending an email with a large total file size of attachments surpassing @threshold for handler @handler (@handler_id) on form @form (@form_id).</p>', [
276276
'@name' => $emailAddress,
277277
'@submission' => $context['link'],
278278
'@handler' => $context['@handler'],

web/modules/custom/os2forms_selvbetjening/os2forms_selvbetjening.services.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
services:
2+
logger.channel.os2forms_selvbetjening:
3+
parent: logger.channel_base
4+
arguments: [ 'os2forms_selvbetjening' ]
25

36
Drupal\os2forms_selvbetjening\Helper\WebformConfigurationExporter:
47
arguments:
@@ -8,6 +11,7 @@ services:
811
Drupal\os2forms_selvbetjening\Helper\FormHelper:
912
arguments:
1013
- '@current_user'
14+
- '@logger.channel.os2forms_selvbetjening'
1115

1216
Drupal\Drupal\os2forms_selvbetjening\Form\SettingsForm:
1317
arguments:

web/modules/custom/os2forms_selvbetjening/src/Helper/FormHelper.php

+89-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\Form\FormStateInterface;
66
use Drupal\Core\Link;
7+
use Drupal\Core\Logger\LoggerChannel;
78
use Drupal\Core\Render\Element;
89
use Drupal\Core\Session\AccountInterface;
910
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -21,8 +22,10 @@ class FormHelper {
2122
*
2223
* @param \Drupal\Core\Session\AccountInterface $account
2324
* Current user.
25+
* @param \Drupal\Core\Logger\LoggerChannel $logger
26+
* Logger.
2427
*/
25-
public function __construct(private readonly AccountInterface $account) {
28+
public function __construct(private readonly AccountInterface $account, private readonly LoggerChannel $logger) {
2629
}
2730

2831
/**
@@ -99,6 +102,91 @@ public function formAlter(array &$form, FormStateInterface $form_state, string $
99102
];
100103
}
101104

105+
if ('template_edit_task' === $form_id) {
106+
$form['#validate'][] = [$this, 'validateByContentFunction'];
107+
}
108+
}
109+
110+
/**
111+
* Validates form input by checking a user-defined function.
112+
*
113+
* This method ensures that:
114+
* - The specified function exists.
115+
* - The number of provided parameters matches
116+
* the defined function's requirements,
117+
* adjusted for additional parameters added during execution.
118+
*
119+
* @param array $form
120+
* The form structure.
121+
* @param \Drupal\Core\Form\FormStateInterface $form_state
122+
* The current state of the form.
123+
*/
124+
public function validateByContentFunction(array &$form, FormStateInterface $form_state): void {
125+
if ('bycontentfunction' === $form_state->getValue(['spv', 'method'])) {
126+
127+
// Get function name and parameters defined in the flow task.
128+
$value = $form_state->getValue(['spv', 'variable_value']);
129+
130+
// Split function name and parameters (format: function:param1,param2).
131+
$functionParts = explode(':', $value, 2);
132+
133+
// Get function name and parameters.
134+
$functionName = $functionParts[0];
135+
$functionParams = isset($functionParts[1]) ? explode(',', $functionParts[1]) : [];
136+
137+
// Get number of parameters.
138+
$paramCount = count($functionParams);
139+
140+
if (!function_exists($functionName)) {
141+
$form_state->setError($form['spv'], $this->t('Function %function_name does not exist', ['%function_name' => $functionName]));
142+
return;
143+
}
144+
145+
// Get the number of parameters for the defined function.
146+
try {
147+
$functionParamCount = (new \ReflectionFunction($functionName))->getNumberOfRequiredParameters();
148+
}
149+
catch (\ReflectionException $e) {
150+
$form_state->setError(
151+
$form['spv'],
152+
$this->t('Invalid function %function_name', [
153+
'%function_name' => $functionName,
154+
])
155+
);
156+
$this->logger->error('Error reflecting function %function_name: %message', [
157+
'%function_name' => $functionName,
158+
'%message' => $e->getMessage(),
159+
// Add the full exception to the context for future reference.
160+
'exception' => $e,
161+
]);
162+
return;
163+
}
164+
165+
// The maestro execute method always adds 2 parameters
166+
// (queueID and processID) when handling the "bycontentfunction" case.
167+
// @see MaestroSetProcessVariableTask::execute()
168+
$functionParamCount -= 2;
169+
170+
if ($functionParamCount < 0) {
171+
$form_state->setError($form['spv'], $this->t('Function %function_name is required to take at least 2 arguments.', ['%function_name' => $functionName]));
172+
return;
173+
}
174+
175+
// Check if the number of parameters matches.
176+
if ($paramCount !== $functionParamCount) {
177+
$form_state->setError(
178+
$form['spv'],
179+
$this->t(
180+
'Function %function_name expects %function_parameter_count parameters. %parameter_defined_count given.',
181+
[
182+
'%function_name' => $functionName,
183+
'%function_parameter_count' => $functionParamCount,
184+
'%parameter_defined_count' => $paramCount,
185+
]
186+
)
187+
);
188+
}
189+
}
102190
}
103191

104192
}

0 commit comments

Comments
 (0)