-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHandlerConfigurationValidator.php
More file actions
112 lines (103 loc) · 3.56 KB
/
Copy pathHandlerConfigurationValidator.php
File metadata and controls
112 lines (103 loc) · 3.56 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
<?php
namespace Drupal\os2forms\Helper;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\os2forms\Form\HandlerConfigurationValidationForm;
use Drupal\webform\Plugin\WebformHandlerInterface;
use Drupal\webform\WebformInterface;
/**
* Validates configuration of webform handlers.
*/
class HandlerConfigurationValidator {
use StringTranslationTrait;
/**
* Constructs a HandlerConfigurationValidator object.
*
* @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
* The form builder.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(
private readonly FormBuilderInterface $formBuilder,
private readonly MessengerInterface $messenger,
) {
}
/**
* Validates configuration of all handlers on a webform.
*
* Handlers are validated by programmatically submitting their
* configuration form with the stored configuration.
*
* @param \Drupal\webform\WebformInterface $webform
* The webform.
*
* @return array
* Problems keyed by handler ID. Each value is an array with two keys:
* - label: the webform handler label
* - problems: a list of problems (strings or markup)
* Handlers without problems are not included.
*
* @phpstan-return array<string, mixed>
*/
public function validate(WebformInterface $webform): array {
$problems = [];
foreach ($webform->getHandlers() as $handler) {
$handlerProblems = $this->validateUsingConfigurationForm($handler);
if ($handlerProblems) {
$problems[$handler->getHandlerId()] = [
'label' => $handler->label(),
'problems' => $handlerProblems,
];
}
}
return $problems;
}
/**
* Validates handler configuration using the handler's configuration form.
*
* The configuration form is submitted programmatically with no input; form
* elements then use their default values, i.e. the stored handler
* configuration, and form validation reports stored values that are no
* longer valid on the current webform, e.g. references to elements that
* have been renamed or deleted (reported as illegal choices).
*
* @param \Drupal\webform\Plugin\WebformHandlerInterface $handler
* The webform handler.
*
* @return array
* A list of problems (strings or markup). An empty list means that the
* configuration is valid.
*
* @phpstan-return array<int, mixed>
*/
private function validateUsingConfigurationForm(WebformHandlerInterface $handler): array {
$formState = new FormState();
// Building and validating the form may add messages, e.g. warnings from
// the handler's own validation, that must not leak to the current page.
// Remember any existing messages and restore them when done.
$messages = $this->messenger->deleteAll();
try {
$this->formBuilder->submitForm(new HandlerConfigurationValidationForm($handler), $formState);
$errors = $formState->getErrors();
}
catch (\Throwable $throwable) {
$errors = [
$this->t('Validating the configuration failed: @message', [
'@message' => $throwable->getMessage(),
]),
];
}
finally {
$this->messenger->deleteAll();
foreach ($messages as $type => $typeMessages) {
foreach ($typeMessages as $message) {
$this->messenger->addMessage($message, $type, TRUE);
}
}
}
return array_values($errors);
}
}