|
5 | 5 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
6 | 6 | use Drupal\file\FileInterface; |
7 | 7 | use Drupal\webform\WebformInterface; |
| 8 | +use Drupal\webform\WebformSubmissionInterface; |
| 9 | +use Drupal\webform_entity_print_attachment\Element\WebformEntityPrintAttachment; |
8 | 10 | use Drupal\webform_rest\Event\WebformSubmissionDataEvent; |
9 | 11 | use Psr\Log\LoggerAwareTrait; |
10 | 12 | use Psr\Log\LoggerInterface; |
@@ -53,6 +55,11 @@ public function onWebformSubmissionDataEvent(WebformSubmissionDataEvent $event): |
53 | 55 | if (!empty($linkedData)) { |
54 | 56 | $event->setData($event->getData() + ['linked' => $linkedData]); |
55 | 57 | } |
| 58 | + |
| 59 | + $attachments = $this->buildAttachments($event->getWebformSubmission(), $event->getData()); |
| 60 | + if (!empty($attachments)) { |
| 61 | + $event->setData($event->getData() + ['attachments' => $attachments]); |
| 62 | + } |
56 | 63 | } |
57 | 64 |
|
58 | 65 | /** |
@@ -115,6 +122,35 @@ private function buildLinked(WebformInterface $webform, array $data): array { |
115 | 122 | return $linked; |
116 | 123 | } |
117 | 124 |
|
| 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 | + |
118 | 154 | /** |
119 | 155 | * {@inheritdoc} |
120 | 156 | */ |
|
0 commit comments