|
2 | 2 |
|
3 | 3 | namespace Drupal\os2forms_digital_post\Plugin\WebformHandler;
|
4 | 4 |
|
5 |
| -use Drupal\Component\Utility\UrlHelper; |
6 | 5 | use Drupal\Core\Form\FormStateInterface;
|
| 6 | +use Drupal\os2forms_digital_post\Helper\MeMoHelper; |
7 | 7 | use Drupal\os2forms_digital_post\Helper\WebformHelperSF1601;
|
8 | 8 | use Drupal\webform\Plugin\WebformHandlerBase;
|
9 | 9 | use Drupal\webform\WebformSubmissionInterface;
|
@@ -137,23 +137,18 @@ public function buildConfigurationForm(array $form, FormStateInterface $formStat
|
137 | 137 | '#description' => $this->t('Remove an action by clearing %action and saving.', ['%action' => (string) $this->t('Action')]),
|
138 | 138 | ];
|
139 | 139 |
|
| 140 | + $form[self::MEMO_ACTIONS]['message'] = [ |
| 141 | + '#markup' => $this->t('<strong>Important</strong>: All action URLs must be absolute and secure, i.e. start with <code>https://</code>.'), |
| 142 | + ]; |
| 143 | + |
140 | 144 | $form[self::MEMO_ACTIONS]['actions'] = [
|
141 | 145 | '#type' => 'table',
|
142 | 146 | ];
|
143 | 147 |
|
144 |
| - $actionOptions = [ |
145 |
| - // @todo Handle SF1601::ACTION_AFTALE. |
146 |
| - SF1601::ACTION_BEKRAEFT => $this->getTranslatedActionName(SF1601::ACTION_BEKRAEFT), |
147 |
| - SF1601::ACTION_BETALING => $this->getTranslatedActionName(SF1601::ACTION_BETALING), |
148 |
| - SF1601::ACTION_FORBEREDELSE => $this->getTranslatedActionName(SF1601::ACTION_FORBEREDELSE), |
149 |
| - SF1601::ACTION_INFORMATION => $this->getTranslatedActionName(SF1601::ACTION_INFORMATION), |
150 |
| - SF1601::ACTION_SELVBETJENING => $this->getTranslatedActionName(SF1601::ACTION_SELVBETJENING), |
151 |
| - SF1601::ACTION_TILMELDING => $this->getTranslatedActionName(SF1601::ACTION_TILMELDING), |
152 |
| - SF1601::ACTION_UNDERSKRIV => $this->getTranslatedActionName(SF1601::ACTION_UNDERSKRIV), |
153 |
| - ]; |
| 148 | + $actionOptions = MeMoHelper::getTranslatedActionNames(); |
154 | 149 | $actions = $this->configuration[self::MEMO_ACTIONS]['actions'] ?? [];
|
155 | 150 | for ($i = 0; $i <= count($actions); $i++) {
|
156 |
| - $action = $actions[$i]; |
| 151 | + $action = $actions[$i] ?? []; |
157 | 152 | $form[self::MEMO_ACTIONS]['actions'][$i]['action'] = [
|
158 | 153 | '#type' => 'select',
|
159 | 154 | '#title' => $this->t('Action'),
|
@@ -276,31 +271,24 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
|
276 | 271 | }
|
277 | 272 | else {
|
278 | 273 | $url = $action['url'];
|
279 |
| - // URL must be absolute and use https (cf. https://digitaliser.dk/digital-post/nyhedsarkiv/2024/nov/oeget-validering-i-digital-post) |
280 |
| - if (!UrlHelper::isValid($url, absolute: TRUE)) { |
281 |
| - $formState->setErrorByName( |
282 |
| - self::MEMO_ACTIONS . '][actions][' . $index . '][url', |
283 |
| - $this->t('Url <code>@url</code> for action %action must be absolute, i.e. start with <code>https://</code>.', [ |
284 |
| - '@url' => $url, |
285 |
| - '%action' => $this->getTranslatedActionName($action['action']), |
286 |
| - ]) |
287 |
| - ); |
| 274 | + // Add warning if URL contains tokens. |
| 275 | + if (preg_match('/\[[a-z_:]+/', $url)) { |
| 276 | + $message = $this->t('Make sure that the tokens in the URL <code>@url</code> for action %action expands to an absolute URL, i.e. something starting with <code>https://</code>.', [ |
| 277 | + '@url' => $url, |
| 278 | + '%action' => MeMoHelper::getTranslatedActionName($action['action']), |
| 279 | + ]); |
| 280 | + $this->messenger()->addWarning($message); |
| 281 | + continue; |
288 | 282 | }
|
289 |
| - elseif ('https' !== parse_url($url, PHP_URL_SCHEME)) { |
290 |
| - $formState->setErrorByName( |
291 |
| - self::MEMO_ACTIONS . '][actions][' . $index . '][url', |
292 |
| - $this->t('Url <code>@url</code> for action %action must use the <code>https</code> scheme, i.e. start with <code>https://</code>.', [ |
293 |
| - '@url' => $url, |
294 |
| - '%action' => $this->getTranslatedActionName($action['action']), |
295 |
| - ]) |
296 |
| - ); |
| 283 | + if ($message = MeMoHelper::validateActionUrl($url, $action)) { |
| 284 | + $formState->setErrorByName(self::MEMO_ACTIONS . '][actions][' . $index . '][url', $message); |
297 | 285 | }
|
298 | 286 | }
|
299 | 287 | if (isset($definedActions[$action['action']])) {
|
300 | 288 | $formState->setErrorByName(
|
301 | 289 | self::MEMO_ACTIONS . '][actions][' . $index . '][action',
|
302 | 290 | $this->t('Action %action already defined.', [
|
303 |
| - '%action' => $this->getTranslatedActionName($action['action']), |
| 291 | + '%action' => MeMoHelper::getTranslatedActionName($action['action']), |
304 | 292 | ])
|
305 | 293 | );
|
306 | 294 | }
|
@@ -359,33 +347,4 @@ public function postPurge(array $webformSubmissions) {
|
359 | 347 | $this->helper->deleteMessages($webformSubmissions);
|
360 | 348 | }
|
361 | 349 |
|
362 |
| - /** |
363 |
| - * Translated action names. |
364 |
| - * |
365 |
| - * @var array|null |
366 |
| - * |
367 |
| - * @phpstan-var array<string, string> |
368 |
| - */ |
369 |
| - private ?array $translatedActionNames = NULL; |
370 |
| - |
371 |
| - /** |
372 |
| - * Get translated action name. |
373 |
| - */ |
374 |
| - private function getTranslatedActionName(string $action): string { |
375 |
| - if (NULL === $this->translatedActionNames) { |
376 |
| - $this->translatedActionNames = [ |
377 |
| - SF1601::ACTION_AFTALE => (string) $this->t('Aftale', [], ['context' => 'memo action']), |
378 |
| - SF1601::ACTION_BEKRAEFT => (string) $this->t('Bekræft', [], ['context' => 'memo action']), |
379 |
| - SF1601::ACTION_BETALING => (string) $this->t('Betaling', [], ['context' => 'memo action']), |
380 |
| - SF1601::ACTION_FORBEREDELSE => (string) $this->t('Forberedelse', [], ['context' => 'memo action']), |
381 |
| - SF1601::ACTION_INFORMATION => (string) $this->t('Information', [], ['context' => 'memo action']), |
382 |
| - SF1601::ACTION_SELVBETJENING => (string) $this->t('Selvbetjening', [], ['context' => 'memo action']), |
383 |
| - SF1601::ACTION_TILMELDING => (string) $this->t('Tilmelding', [], ['context' => 'memo action']), |
384 |
| - SF1601::ACTION_UNDERSKRIV => (string) $this->t('Underskriv', [], ['context' => 'memo action']), |
385 |
| - ]; |
386 |
| - } |
387 |
| - |
388 |
| - return $this->translatedActionNames[$action] ?? $action; |
389 |
| - } |
390 |
| - |
391 | 350 | }
|
0 commit comments