Skip to content

Commit c9bf867

Browse files
committed
Cleaned up preview
1 parent 22b0480 commit c9bf867

6 files changed

Lines changed: 59 additions & 13 deletions

File tree

src/Controller/Os2formsFordelingskomponentDistributionObjectPreviewController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function renderPreview(WebformHandlerSF2900 $handler, HandlerSettings $ha
9595
'exceptions' => $exceptions,
9696
'warnings' => $warnings,
9797
'distribution_object' => $distributionObject,
98-
'xml' => $xml,
98+
'xml' => $xml->withContextAsArray(),
9999
];
100100
}
101101

src/Helper/FordelingskomponentHelper.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ private function buildDistributionFormularType(
264264
Attachment $attachment,
265265
): DistributionFormular {
266266
$files = $this->buildFileGroups($handlerSettings, $submission);
267-
$xml = $this->renderXml($handlerSettings, $submission, $files)->rendered;
267+
$renderResult = $this->renderXml($handlerSettings, $submission, $files);
268+
if ($renderResult->exception) {
269+
throw $renderResult->exception;
270+
}
271+
272+
$xml = (string) $renderResult->rendered;
268273
$xsdUrl = $handlerSettings->distributionObject->xsdUrl;
269274

270275
$this->xmlHelper->validateXml($xml);
@@ -319,10 +324,20 @@ public function renderXml(
319324

320325
$context = $this->xmlHelper->getRenderContext($handlerSettings, $submission, $files);
321326

327+
$rendered = NULL;
328+
$exception = NULL;
329+
try {
330+
$rendered = $this->xmlHelper->render($template, $context, validateXml: $validateXml);
331+
}
332+
catch (\Exception $e) {
333+
$exception = $e;
334+
}
335+
322336
return new XmlRenderResult(
323-
rendered: $this->xmlHelper->render($template, $context, validateXml: $validateXml),
324337
template: $template,
325338
context: $context,
339+
rendered: $rendered,
340+
exception: $exception,
326341
);
327342
}
328343

@@ -349,7 +364,7 @@ public function buildFileGroups(HandlerSettings $handlerSettings, WebformSubmiss
349364
$values = $submission->getData()[$type] ?? NULL;
350365
if ($values) {
351366
/** @var \Drupal\file\FileInterface[] $files */
352-
$files = $this->fileStorage->loadMultiple($values);
367+
$files = $this->fileStorage->loadMultiple((array) $values);
353368
foreach ($files as $file) {
354369
$groups[$type][] = [
355370
'sftp_filename' => $this->getSftpFilename($handlerSettings, $submission, $file->getFilename()),

src/Helper/XmlHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,9 @@ public function render(string $template, array $context, bool $validateXml = TRU
7070
*/
7171
public function getRenderContext(HandlerSettings $handlerSettings, WebformSubmissionInterface $submission, array $files) {
7272
return [
73-
'handler' => ['settings' => $handlerSettings],
7473
'submission' => $submission,
75-
'webform_submission' => $submission,
7674
'files' => $files,
75+
'handler' => ['settings' => $handlerSettings->toArray()],
7776
];
7877
}
7978

src/Model/XmlRenderResult.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Drupal\os2forms_fordelingskomponent\Model;
44

5+
use Drupal\webform\WebformSubmissionInterface;
6+
57
/**
68
* The Document class.
79
*/
@@ -11,10 +13,29 @@
1113
* Constructor.
1214
*/
1315
public function __construct(
14-
public string $rendered,
1516
public string $template,
1617
public array $context,
18+
public ?string $rendered,
19+
public ?\Exception $exception,
1720
) {
1821
}
1922

23+
/**
24+
* Convert objects in context to arrays.
25+
*/
26+
public function withContextAsArray(): self {
27+
$context = $this->context;
28+
if (($context['submission'] ?? NULL) instanceof WebformSubmissionInterface) {
29+
$context['submission'] = $context['submission']->toArray(TRUE, TRUE);
30+
}
31+
// @todo Convert 'handler' and 'files'
32+
return new self(
33+
$this->template,
34+
$context,
35+
$this->rendered,
36+
$this->exception,
37+
);
38+
39+
}
40+
2041
}

src/Settings/AbstractSettings.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,18 @@ public function apply(array $values, bool $throwExceptionOnMissingProperty = FAL
114114
/**
115115
* Convert settings to array.
116116
*/
117-
public function toArray(): array {
118-
return $this->values;
117+
public function toArray(bool $recursive = true): array {
118+
$values = $this->values;
119+
120+
if ($recursive) {
121+
foreach ($values as &$value) {
122+
if ($value instanceof self) {
123+
$value = $value->toArray($recursive);
124+
}
125+
}
126+
}
127+
128+
return $values;
119129
}
120130

121131
/**

templates/base.html.twig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@
8383
<fieldset>
8484
<legend>{{ label }}</legend>
8585

86+
{#
8687
{% set data_url = 'data:application/json,' ~ (value|json_encode|url_encode) %}
8788
<iframe src="{{ data_url }}"></iframe>
8889
<a target="_blank" href="{{ data_url }}">{{ 'Open data in new tab'|trans }}</a>
90+
#}
8991

9092
<pre><code>{{ value|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_UNESCAPED_UNICODE')) }}</code></pre>
9193
</fieldset>
@@ -95,13 +97,12 @@
9597
<fieldset>
9698
<legend>{{ label }}</legend>
9799

100+
{#
98101
{% set data_url = 'data:text/xml,' ~ (value|url_encode) %}
99102
<iframe src="{{ data_url }}"></iframe>
100103
<a target="_blank" href="{{ data_url }}">{{ 'Open data in new tab'|trans }}</a>
104+
#}
101105

102-
<details>
103-
<summary>{{ 'Raw content'|trans }}</summary>
104-
<pre><code>{{ value }}</code></pre>
105-
</details>
106+
<pre><code>{{ value }}</code></pre>
106107
</fieldset>
107108
{% endmacro %}

0 commit comments

Comments
 (0)