Skip to content

Commit 51f3f9c

Browse files
committed
Added more form rendering error handling
1 parent 4e28d2e commit 51f3f9c

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

.github/workflows/pr.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
runs-on: ubuntu-latest
2424
strategy:
2525
matrix:
26-
php-versions: [ '7.4', '8.0', '8.1' ]
26+
php-versions: [ '8.1' ]
2727
dependency-version: [ prefer-lowest, prefer-stable ]
2828
steps:
2929
- uses: actions/checkout@master
@@ -55,7 +55,7 @@ jobs:
5555
runs-on: ubuntu-latest
5656
strategy:
5757
matrix:
58-
php-versions: [ '7.4', '8.0', '8.1' ]
58+
php-versions: [ '8.1' ]
5959
dependency-version: [ prefer-lowest, prefer-stable ]
6060
steps:
6161
- uses: actions/checkout@master
@@ -88,7 +88,7 @@ jobs:
8888
runs-on: ubuntu-latest
8989
strategy:
9090
matrix:
91-
php-versions: [ '7.4', '8.0', '8.1' ]
91+
php-versions: [ '8.1' ]
9292
dependency-version: [ prefer-lowest, prefer-stable ]
9393
steps:
9494
- uses: actions/checkout@master

.php-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8.1

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Versioning](https://semver.org/spec/v2.0.0.html).
99

1010
## [Unreleased]
1111

12+
### Fixed
13+
14+
- Handled even more errors in form preview
15+
1216
## [1.1.1]
1317

1418
### Fixed

src/Controller/WebformController.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Drupal\Core\Http\RequestStack;
88
use Drupal\Core\Link;
99
use Drupal\Core\Render\Markup;
10+
use Drupal\Core\Render\RendererInterface;
1011
use Drupal\Core\Routing\TrustedRedirectResponse;
1112
use Drupal\Core\Serialization\Yaml;
1213
use Drupal\Core\Url;
@@ -24,34 +25,15 @@ final class WebformController extends ControllerBase {
2425
private const FILTER_WEBFORMS_IMPORTED = 'imported';
2526
private const FILTER_WEBFORMS_NOT_IMPORTED = 'not imported';
2627

27-
/**
28-
* The request stack.
29-
*
30-
* @var \Drupal\Core\Http\RequestStack
31-
*/
32-
private RequestStack $requestStack;
33-
34-
/**
35-
* The import helper.
36-
*
37-
* @var \Drupal\os2forms_sync\Helper\ImportHelper
38-
*/
39-
private ImportHelper $importHelper;
40-
41-
/**
42-
* The webform helper.
43-
*
44-
* @var \Drupal\os2forms_sync\Helper\WebformHelper
45-
*/
46-
private WebformHelper $webformHelper;
47-
4828
/**
4929
* Constructor.
5030
*/
51-
public function __construct(RequestStack $requestStack, ImportHelper $importHelper, WebformHelper $webformHelper) {
52-
$this->requestStack = $requestStack;
53-
$this->importHelper = $importHelper;
54-
$this->webformHelper = $webformHelper;
31+
public function __construct(
32+
readonly private RequestStack $requestStack,
33+
readonly private ImportHelper $importHelper,
34+
readonly private WebformHelper $webformHelper,
35+
readonly private RendererInterface $renderer
36+
) {
5537
}
5638

5739
/**
@@ -61,7 +43,8 @@ public static function create(ContainerInterface $container): self {
6143
return new static(
6244
$container->get('request_stack'),
6345
$container->get(ImportHelper::class),
64-
$container->get(WebformHelper::class)
46+
$container->get(WebformHelper::class),
47+
$container->get('renderer')
6548
);
6649
}
6750

@@ -141,6 +124,9 @@ public function index(): array {
141124
$attributes = $webform->attributes;
142125
try {
143126
$form = $this->webformHelper->getSubmissionForm($attributes['elements']);
127+
// Make sure that the form cannot be submitted (hopefully).
128+
$form['#attributes']['onsubmit'] = 'return false';
129+
$renderedForm = $this->renderer->renderPlain($form);
144130
}
145131
catch (\Throwable $t) {
146132
$form = [
@@ -151,9 +137,8 @@ public function index(): array {
151137
],
152138
],
153139
];
140+
$renderedForm = $this->renderer->renderPlain($form);
154141
}
155-
// Make sure that the form cannot be submitted (hopefully).
156-
$form['#attributes']['onsubmit'] = 'return false';
157142

158143
$sourceUrl = $webform->sourceUrl;
159144
$importedWebform = $importedWebforms[$sourceUrl] ?? NULL;
@@ -178,7 +163,9 @@ public function index(): array {
178163
'form_display' => [
179164
'#type' => 'details',
180165
'#title' => $this->t('Form display'),
181-
'form' => $form,
166+
'form' => [
167+
'#markup' => $renderedForm,
168+
],
182169
],
183170

184171
'elements' => [

0 commit comments

Comments
 (0)