Skip to content

Commit 11e1b6d

Browse files
committed
Improvements
1 parent 65cc032 commit 11e1b6d

5 files changed

Lines changed: 108 additions & 20 deletions

File tree

docs/Users-manual.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,26 @@ The render context contains three main parts:
239239
2. Information on files attached (uploaded) to the submission (grouped by element key)
240240
3. The handler settings: This includes the global OS2Forms Fordelingskomponent module settings and the settings on the
241241
handler
242+
243+
## Twig extensions
244+
245+
This modules adds some useful Twig functions,
246+
247+
`os2forms_fordelingskomponent_intval`
248+
`os2forms_fordelingskomponent_floatval`
249+
`os2forms_fordelingskomponent_gettype`
250+
251+
that basically wrap the build PHP functions [`intval`](https://www.php.net/manual/en/function.intval.php),
252+
[`floatval`](https://www.php.net/manual/en/function.floatval.php) and
253+
[`gettype`](https://www.php.net/manual/en/function.gettype.php).
254+
255+
However, `os2forms_fordelingskomponent_floatval` accepts a second argument `langcode` that's used to help extract the
256+
float value based on a language code:
257+
258+
``` twig
259+
{{ os2forms_fordelingskomponent_floatval('1,23') == 123 }}
260+
{{ os2forms_fordelingskomponent_floatval('1.23') == 1.23 }}
261+
262+
{{ os2forms_fordelingskomponent_floatval('1,23', langcode: 'da') == 1.23 }}
263+
{{ os2forms_fordelingskomponent_floatval('1.23', langcode: 'da') == 123}}
264+
```

os2forms_fordelingskomponent.services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ services:
2929
Drupal\os2forms_fordelingskomponent\Repository\AnvenderKvitteringRepository:
3030

3131
Drupal\os2forms_fordelingskomponent\Settings:
32+
33+
Drupal\os2forms_fordelingskomponent\Os2formsFordelingskomponentTwigExtension:
34+
tags:
35+
- { name: twig.extension }

src/Drush/Commands/ValidateXmlCommand.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function configure(): void {
5858
->addArgument('webform-id', InputArgument::REQUIRED, 'Webform ID')
5959
->addArgument('handler-id', InputArgument::REQUIRED, 'Handler ID')
6060
->addOption('show-xml', NULL, InputOption::VALUE_NONE, 'Show XML')
61-
->addOption('break-on-error', NULL, InputOption::VALUE_OPTIONAL, 'Break on error. If set, terminate after first error.', TRUE);
61+
->addOption('break-on-error', NULL, InputOption::VALUE_OPTIONAL, 'Break on error. If set, terminate after first error.', FALSE);
6262
}
6363

6464
/**
@@ -78,29 +78,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7878
$handler = $this->getHandler($handlerId, $webform);
7979
$submissions = $this->loadSubmissions($webform);
8080

81-
foreach ($submissions as $submission) {
82-
$preview = $this->webformHelper->renderPreview($handler, $submission);
83-
$hasErrors = count($preview['exceptions']) > 0;
84-
if ($hasErrors) {
85-
foreach ($preview['exceptions'] as $exception) {
86-
$io->error([$submission->label(), $exception->getMessage()]);
81+
if (0 === count($submissions)) {
82+
$io->warning(sprintf('No submissions on form %s', $webform->label()));
83+
}
84+
else {
85+
foreach ($submissions as $submission) {
86+
$preview = $this->webformHelper->renderPreview($handler, $submission);
87+
$hasErrors = count($preview['exceptions']) > 0;
88+
if ($hasErrors) {
89+
foreach ($preview['exceptions'] as $exception) {
90+
$io->error([$submission->label(), $exception->getMessage()]);
91+
}
92+
}
93+
else {
94+
$io->success($submission->label());
8795
}
88-
}
89-
else {
90-
$io->success($submission->label());
91-
}
9296

93-
if ($showXml) {
94-
$io->writeln($preview['xml']->rendered);
95-
}
97+
if ($showXml) {
98+
if (NULL === $preview['xml']->rendered) {
99+
$io->warning('Cannot render XML');
100+
}
101+
else {
102+
$io->writeln($preview['xml']->rendered);
103+
}
104+
}
96105

97-
if ($hasErrors) {
98-
if ($breakOnError) {
99-
return self::FAILURE;
106+
if ($hasErrors) {
107+
if ($breakOnError) {
108+
return self::FAILURE;
109+
}
110+
continue;
100111
}
101-
continue;
102112
}
103-
104113
}
105114

106115
return self::SUCCESS;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\os2forms_fordelingskomponent;
6+
7+
use Twig\Extension\AbstractExtension;
8+
use Twig\TwigFunction;
9+
10+
/**
11+
* Twig extension.
12+
*/
13+
final class Os2formsFordelingskomponentTwigExtension extends AbstractExtension {
14+
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getFunctions(): array {
19+
$functions[] = new TwigFunction(
20+
'os2forms_fordelingskomponent_intval',
21+
intval(...),
22+
);
23+
24+
$functions[] = new TwigFunction(
25+
'os2forms_fordelingskomponent_floatval',
26+
static function (string $argument, ?string $langcode = NULL): float {
27+
$replacements = match ($langcode) {
28+
// Remove group separators and replace comma with point.
29+
'da' => ['.' => '', ',' => '.'],
30+
// Remove group separators.
31+
default => [',' => ''],
32+
};
33+
34+
$argument = str_replace(array_keys($replacements), array_values($replacements), $argument);
35+
36+
return floatval($argument);
37+
}
38+
);
39+
40+
$functions[] = new TwigFunction(
41+
'os2forms_fordelingskomponent_gettype',
42+
gettype(...),
43+
);
44+
45+
return $functions;
46+
}
47+
48+
}

templates/base.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959
<span disabled>{{ 'Previous submission'|t }}</span>
6060
{% endif %}
6161

62-
<a href="{{ path('entity.webform_submission.yaml', {webform: submission.webform.id, webform_submission: submission.id}) }}">{{ 'Show submission'|t }}</a>
62+
<div class="submission">
63+
<a href="{{ path('entity.webform_submission.yaml', {webform: submission.webform.id, webform_submission: submission.id}) }}">{{ submission.label }}</a>
64+
[<a href="{{ path('entity.webform_submission.edit_form.all', {webform: submission.webform.id, webform_submission: submission.id}) }}">{{ 'Edit'|t }}</a>]
65+
</div>
66+
6367
{% if links.next %}
6468
<a href="{{ links.next }}">{{ 'Next submission'|t }}</a>
6569
{% else %}

0 commit comments

Comments
 (0)