-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebformSubmissionDataEventSubscriber.php
172 lines (151 loc) · 5.19 KB
/
WebformSubmissionDataEventSubscriber.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Drupal\os2forms_rest_api\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\file\FileInterface;
use Drupal\os2forms_attachment\Element\AttachmentElement;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_entity_print_attachment\Element\WebformEntityPrintAttachment;
use Drupal\webform_rest\Event\WebformSubmissionDataEvent;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* WebformSubmissionEventSubscriber, for updating Webform Submission GET data.
*/
class WebformSubmissionDataEventSubscriber implements EventSubscriberInterface {
use LoggerAwareTrait;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private EntityTypeManagerInterface $entityTypeManager;
/**
* Map from entity type to webform element types.
*/
private const LINKED_ELEMENT_TYPES = [
'file' => [
'webform_image_file',
'webform_document_file',
'webform_video_file',
'webform_audio_file',
'managed_file',
],
];
/**
* Constructor.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, LoggerInterface $logger) {
$this->entityTypeManager = $entityTypeManager;
$this->setLogger($logger);
}
/**
* Event handler.
*/
public function onWebformSubmissionDataEvent(WebformSubmissionDataEvent $event): void {
$linkedData = $this->buildLinked($event->getWebformSubmission()->getWebform(), $event->getData());
if (!empty($linkedData)) {
$event->setData($event->getData() + ['linked' => $linkedData]);
}
$attachments = $this->buildAttachments($event->getWebformSubmission(), $event->getData());
if (!empty($attachments)) {
$event->setData($event->getData() + ['attachments' => $attachments]);
}
}
/**
* Builds linked entity data.
*
* This is heavily inspired by “Sideloading” in Deskpro
* (https://support.deskpro.com/en-US/guides/developers/deskpro-api/basics/sideloading).
*
* @phpstan-param array<string, mixed> $data
* @phpstan-return array<string, mixed>
*/
private function buildLinked(WebformInterface $webform, array $data): array {
$linked = [];
$elements = $webform->getElementsDecodedAndFlattened();
foreach ($elements as $name => $element) {
if (!isset($data[$name])) {
continue;
}
$linkedEntityType = NULL;
if (isset($element['#target_type'])) {
$linkedEntityType = $element['#target_type'];
}
else {
foreach (self::LINKED_ELEMENT_TYPES as $entityType => $elementTypes) {
if (in_array($element['#type'], $elementTypes, TRUE)) {
$linkedEntityType = $entityType;
break;
}
}
}
if (NULL !== $linkedEntityType) {
// $data[$name] is either a string id i.e. '127',
// or an array of string ids i.e. ['127', '128'].
// Casting to array allow us to handle both cases the same way.
$values = (array) $data[$name];
$entities = $this->entityTypeManager->getStorage($linkedEntityType)->loadMultiple($values);
foreach ($entities as $value => $entity) {
$link = [];
if ($entity instanceof FileInterface) {
$link = [
'id' => $entity->id(),
'url' => $entity->createFileUrl(FALSE),
'mime_type' => $entity->getMimeType(),
'size' => $entity->getSize(),
];
}
else {
$this->logger->warning(sprintf('Unhandled linked entity type %s', $linkedEntityType));
}
if (!empty($link)) {
$linked[$name][$value] = $link;
}
}
}
}
return $linked;
}
/**
* Builds attachment data.
*
* @phpstan-param array<string, mixed> $data
* @phpstan-return array<string, mixed>
*/
private function buildAttachments(WebformSubmissionInterface $submission, array $data): array {
$attachments = [];
$webform = $submission->getWebform();
$attachmentElements = $webform->getElementsAttachments();
foreach ($attachmentElements as $key => $name) {
$element = $webform->getElement($key);
if (preg_match('/^webform_entity_print_attachment:(?<type>.+)/', $element['#type'] ?? '', $matches)) {
$type = $matches['type'];
$url = WebformEntityPrintAttachment::getFileUrl($element, $submission);
$attachments[$key] = [
'name' => $element['#title'] ?? $name,
'type' => $type,
'url' => $url->toString(TRUE)->getGeneratedUrl(),
];
}
elseif ('os2forms_attachment' === $element['#type']) {
$url = AttachmentElement::getFileUrl($element, $submission);
$attachments[$key] = [
'name' => $element['#title'] ?? $name,
'type' => $element['#export_type'] ?? '',
'url' => $url->toString(TRUE)->getGeneratedUrl(),
];
}
}
return $attachments;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
WebformSubmissionDataEvent::class => ['onWebformSubmissionDataEvent'],
];
}
}