-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHandlerConfigurationValidationSubscriber.php
More file actions
93 lines (82 loc) · 2.73 KB
/
Copy pathHandlerConfigurationValidationSubscriber.php
File metadata and controls
93 lines (82 loc) · 2.73 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
<?php
namespace Drupal\os2forms\EventSubscriber;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\os2forms\Helper\HandlerConfigurationValidator;
use Drupal\webform\WebformInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Reports misconfigured webform handlers on webform admin pages.
*
* @see \Drupal\os2forms\Helper\HandlerConfigurationValidator
*/
class HandlerConfigurationValidationSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Routes on which to report misconfigured handlers.
*/
private const ROUTE_NAMES = [
// The webform build page (where elements are renamed or deleted).
'entity.webform.edit_form',
// The webform handlers page.
'entity.webform.handlers',
];
/**
* Constructs the HandlerConfigurationValidationSubscriber.
*
* @param \Drupal\os2forms\Helper\HandlerConfigurationValidator $validator
* The handler configuration validator.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The current route match.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
*/
public function __construct(
private readonly HandlerConfigurationValidator $validator,
private readonly RouteMatchInterface $routeMatch,
private readonly MessengerInterface $messenger,
) {
}
/**
* Reports misconfigured webform handlers to the current user.
*
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
* The request event.
*/
public function onRequest(RequestEvent $event): void {
if (!in_array($this->routeMatch->getRouteName(), self::ROUTE_NAMES, TRUE)) {
return;
}
// Only report on regular page views.
$request = $event->getRequest();
if (!$request->isMethod('GET') || $request->isXmlHttpRequest()) {
return;
}
$webform = $this->routeMatch->getParameter('webform');
if (!$webform instanceof WebformInterface) {
return;
}
foreach ($this->validator->validate($webform) as $handlerId => $result) {
foreach ($result['problems'] as $problem) {
$this->messenger->addWarning($this->t('The handler @handler (@id) is misconfigured: @problem', [
'@handler' => $result['label'],
'@id' => $handlerId,
'@problem' => $problem,
]));
}
}
}
/**
* {@inheritdoc}
*
* @phpstan-return array<string, mixed>
*/
public static function getSubscribedEvents(): array {
return [
KernelEvents::REQUEST => ['onRequest'],
];
}
}