Skip to content

Commit 5cacdbe

Browse files
authored
Merge pull request #3 from itk-dev/feature/attachments-data
Attachments data
2 parents dc32e46 + 9138097 commit 5cacdbe

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,28 @@ To make using the REST API easier we add linked data to `GET` responses:
169169
}
170170
```
171171

172-
### Technical details on linked data
172+
## Attachments
173+
174+
Attachment elements are added to `GET` responses:
175+
176+
```json
177+
{
178+
179+
"data": {
180+
181+
"attachments": {
182+
"attachment_pdf": {
183+
"name": "Attachment (pdf)",
184+
"type": "pdf",
185+
"url": "http://os2forms.example.com/da/webform/os2forms/submissions/42/attachment/pdf/pdf.pdf"
186+
},
187+
188+
}
189+
}
190+
}
191+
```
192+
193+
### Technical details on linked data and attachments
173194

174195
In order to add linked data, we apply a patch,
175196
[webform_rest_submission.patch](patches/webform_rest_submission.patch), to the

src/EventSubscriber/WebformSubmissionDataEventSubscriber.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Drupal\Core\Entity\EntityTypeManagerInterface;
66
use Drupal\file\FileInterface;
77
use Drupal\webform\WebformInterface;
8+
use Drupal\webform\WebformSubmissionInterface;
9+
use Drupal\webform_entity_print_attachment\Element\WebformEntityPrintAttachment;
810
use Drupal\webform_rest\Event\WebformSubmissionDataEvent;
911
use Psr\Log\LoggerAwareTrait;
1012
use Psr\Log\LoggerInterface;
@@ -53,6 +55,11 @@ public function onWebformSubmissionDataEvent(WebformSubmissionDataEvent $event):
5355
if (!empty($linkedData)) {
5456
$event->setData($event->getData() + ['linked' => $linkedData]);
5557
}
58+
59+
$attachments = $this->buildAttachments($event->getWebformSubmission(), $event->getData());
60+
if (!empty($attachments)) {
61+
$event->setData($event->getData() + ['attachments' => $attachments]);
62+
}
5663
}
5764

5865
/**
@@ -115,6 +122,35 @@ private function buildLinked(WebformInterface $webform, array $data): array {
115122
return $linked;
116123
}
117124

125+
/**
126+
* Builds attachment data.
127+
*
128+
* @phpstan-param array<string, mixed> $data
129+
* @phpstan-return array<string, mixed>
130+
*/
131+
private function buildAttachments(WebformSubmissionInterface $submission, array $data): array {
132+
$attachments = [];
133+
134+
$webform = $submission->getWebform();
135+
$attachmentElements = $webform->getElementsAttachments();
136+
137+
foreach ($attachmentElements as $key => $name) {
138+
$element = $webform->getElement($key);
139+
140+
if (preg_match('/^webform_entity_print_attachment:(?<type>.+)/', $element['#type'] ?? '', $matches)) {
141+
$type = $matches['type'];
142+
$url = WebformEntityPrintAttachment::getFileUrl($element, $submission);
143+
$attachments[$key] = [
144+
'name' => $element['#title'] ?? $name,
145+
'type' => $type,
146+
'url' => $url->toString(TRUE)->getGeneratedUrl(),
147+
];
148+
}
149+
}
150+
151+
return $attachments;
152+
}
153+
118154
/**
119155
* {@inheritdoc}
120156
*/

0 commit comments

Comments
 (0)