-
Notifications
You must be signed in to change notification settings - Fork 1
/
webform_rest_submission.patch
107 lines (101 loc) · 3.25 KB
/
webform_rest_submission.patch
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
diff --git a/src/Event/WebformSubmissionDataEvent.php b/src/Event/WebformSubmissionDataEvent.php
new file mode 100644
index 0000000..c378f45
--- /dev/null
+++ b/src/Event/WebformSubmissionDataEvent.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Drupal\webform_rest\Event;
+
+use Drupal\Component\EventDispatcher\Event;
+use Drupal\webform\WebformSubmissionInterface;
+
+/**
+ * Class WebformSubmissionDataEvent, an event to modify the data part of the response sent from calling GET webform submission.
+ */
+class WebformSubmissionDataEvent extends Event
+{
+ /**
+ * @var WebformSubmissionInterface
+ */
+ private WebformSubmissionInterface $webformSubmission;
+
+ /**
+ * @var array
+ */
+ private array $data;
+
+ /**
+ * Construct for injection dependency.
+ *
+ * @param array $data
+ * @param WebformSubmissionInterface $webformSubmission
+ */
+ public function __construct(WebformSubmissionInterface $webformSubmission, array $data) {
+ $this->webformSubmission = $webformSubmission;
+ $this->setData($data);
+ }
+
+ /**
+ * @return WebformSubmissionInterface
+ */
+ public function getWebformSubmission(): WebformSubmissionInterface
+ {
+ return $this->webformSubmission;
+ }
+
+ public function getData(): array
+ {
+ return $this->data;
+ }
+
+ public function setData(array $data)
+ {
+ $this->data = $data;
+ return $this;
+ }
+}
diff --git a/src/Plugin/rest/resource/WebformSubmissionResource.php b/src/Plugin/rest/resource/WebformSubmissionResource.php
index ebe22aa..4102129 100644
--- a/src/Plugin/rest/resource/WebformSubmissionResource.php
+++ b/src/Plugin/rest/resource/WebformSubmissionResource.php
@@ -5,6 +5,7 @@ namespace Drupal\webform_rest\Plugin\rest\resource;
use Drupal\webform\WebformSubmissionForm;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ModifiedResourceResponse;
+use Drupal\webform_rest\Event\WebformSubmissionDataEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -43,6 +44,13 @@ class WebformSubmissionResource extends ResourceBase {
protected $currentUser;
+ /**
+ * An event dispatcher instance to use for dispatching events.
+ *
+ * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
+ */
+ protected $eventDispatcher;
+
/**
* {@inheritdoc}
*/
@@ -51,6 +59,7 @@ class WebformSubmissionResource extends ResourceBase {
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->request = $container->get('request_stack');
$instance->currentUser = $container->get('current_user');
+ $instance->eventDispatcher = $container->get('event_dispatcher');
return $instance;
}
@@ -110,9 +119,13 @@ class WebformSubmissionResource extends ResourceBase {
// Grab submission data.
$data = $webform_submission->getData();
+ // Dispatch WebformSubmissionDataEvent to allow modification of data.
+ $event = new WebformSubmissionDataEvent($webform_submission, $data);
+ $this->eventDispatcher->dispatch($event);
+
$response = [
'entity' => $webform_submission,
- 'data' => $data,
+ 'data' => $event->getData(),
];
// Return the submission.