Skip to content

Commit 7811196

Browse files
committed
Rework for improved Contao 5 compatibility
- Remove custom success message - Inject error message inside the form so that it works with ajax forms
1 parent c1bece5 commit 7811196

11 files changed

Lines changed: 1475 additions & 1405 deletions

.ddev/contao/composer.lock

Lines changed: 1362 additions & 1260 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.phpcq.yaml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ phpcq:
4343
- 31C7E470E2138192
4444
- 5E6DDE998AB73B8E
4545
- A978220305CD5C32
46+
- 97B02DD8E5071466
4647
tasks:
4748
fix:
4849
- composer-normalize-fix

companion.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"receipts": [
3-
"projects/contao-bundle/4.13-5.3"
3+
"projects/contao-bundle/5.3"
44
],
55
"config": {
6-
"phpConstraint": "^8.1"
6+
"phpConstraint": "^8.2"
77
},
88
"tools": {
99
"composer": {

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@
4040
"source": "https://github.com/hofff/contao-formtools"
4141
},
4242
"require": {
43-
"php": "^8.1",
43+
"php": "^8.2",
4444
"ext-json": "*",
45-
"contao/core-bundle": "^4.13 || ^5.3",
46-
"symfony/config": "^5.4 || ^6.4 || ^7.0",
47-
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
48-
"symfony/http-foundation": "^5.4 || ^6.4 || ^7.0",
49-
"symfony/http-kernel": "^5.4 || ^6.4 || ^7.0"
45+
"contao/core-bundle": "^5.3",
46+
"symfony/config": "^6.4 || ^7.0",
47+
"symfony/dependency-injection": "^6.4 || ^7.0",
48+
"symfony/http-foundation": "^6.4 || ^7.0",
49+
"symfony/http-kernel": "^6.4 || ^7.0"
5050
},
5151
"require-dev": {
5252
"contao/manager-plugin": "^2.1",
@@ -74,8 +74,8 @@
7474
},
7575
"extra": {
7676
"branch-alias": {
77-
"dev-develop": "3.3.x-dev",
78-
"dev-master": "3.2.x-dev",
77+
"dev-develop": "4.0.x-dev",
78+
"dev-master": "3.3.x-dev",
7979
"dev-support/2.x": "2.0.x-dev"
8080
},
8181
"contao-manager-plugin": "Hofff\\Contao\\FormTools\\ContaoManager\\Plugin"

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<arg name="colors"/>
88

99
<!-- Required for the SlevomatCodingStandard which utilizes composer dependencies -->
10-
<autoload>vendor/autoload.php</autoload>
10+
<autoload>.phpcq/plugins/doctrine-coding-standard/vendor/autoload.php</autoload>
1111

1212
<!-- Ignore warnings, show progress of the run and show sniff names -->
1313
<arg value="nps"/>

src/EventListener/Hook/AddErrorMessageListener.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ private function match(Template $template): bool
5252
return false;
5353
}
5454

55-
if (! str_starts_with($template->getName(), 'form_wrapper')) {
55+
if (
56+
! str_starts_with($template->getName(), 'form_wrapper')
57+
&& ! str_starts_with($template->getName(), 'form_inline')
58+
) {
5659
return false;
5760
}
5861

src/EventListener/Hook/AddSuccessMessageListener.php

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Hofff\Contao\FormTools\EventListener\Hook;
6+
7+
use Contao\ContentModel;
8+
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
9+
use Contao\FrontendTemplate;
10+
use Contao\ModuleModel;
11+
use Symfony\Component\HttpFoundation\RequestStack;
12+
use Symfony\Component\HttpFoundation\Session\Session;
13+
14+
use function implode;
15+
use function str_replace;
16+
17+
final class InjectFormMessagesListener
18+
{
19+
public function __construct(private readonly RequestStack $requestStack)
20+
{
21+
}
22+
23+
#[AsHook('getFrontendModule')]
24+
#[AsHook('getContentElement')]
25+
public function onGetModuleOrElement(ContentModel|ModuleModel $element, string $buffer): string
26+
{
27+
if ($element->type !== 'form') {
28+
return $buffer;
29+
}
30+
31+
$errors = $this->getErrorMessages((string) $element->form);
32+
if ($errors === null) {
33+
return $buffer;
34+
}
35+
36+
return str_replace(
37+
'<div class="formbody">',
38+
implode("\n", $errors) . "\n" . '<div class="formbody">',
39+
$buffer,
40+
);
41+
}
42+
43+
#[AsHook('parseFrontendTemplate')]
44+
public function onParseAjaxForTemplate(string $buffer, string $templateName, FrontendTemplate $template): string
45+
{
46+
if ($templateName !== 'form_inline') {
47+
return $buffer;
48+
}
49+
50+
$request = $this->requestStack->getCurrentRequest();
51+
if (! $request || ! $request->isXmlHttpRequest()) {
52+
return $buffer;
53+
}
54+
55+
$errors = $this->getErrorMessages((string) $template->id);
56+
if ($errors === null) {
57+
return $buffer;
58+
}
59+
60+
return str_replace(
61+
'<div class="formbody">',
62+
implode("\n", $errors) . "\n" . '<div class="formbody">',
63+
$buffer,
64+
);
65+
}
66+
67+
/**
68+
* @return list<string>|null
69+
*
70+
* @psalm-suppress MoreSpecificReturnType
71+
* @psalm-suppress LessSpecificReturnStatement
72+
*/
73+
private function getErrorMessages(string $formId): array|null
74+
{
75+
$request = $this->requestStack->getCurrentRequest();
76+
if (! $request) {
77+
return null;
78+
}
79+
80+
$session = $request->getSession();
81+
if (! $session instanceof Session) {
82+
return null;
83+
}
84+
85+
$flashBag = $session->getFlashBag();
86+
$key = 'hofff_formtools_' . $formId;
87+
88+
if (! $flashBag->has($key)) {
89+
return null;
90+
}
91+
92+
return $flashBag->get($key);
93+
}
94+
}

src/EventListener/Hook/PrependFormMessagesListener.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/Resources/config/listener.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
<argument type="service" id="request_stack"/>
99
<argument type="service" id="contao.security.token_checker"/>
1010
</service>
11-
<service id="Hofff\Contao\FormTools\EventListener\Hook\AddSuccessMessageListener" autoconfigure="true">
12-
<argument type="service" id="request_stack"/>
13-
</service>
14-
<service id="Hofff\Contao\FormTools\EventListener\Hook\PrependFormMessagesListener" autoconfigure="true">
11+
<service id="Hofff\Contao\FormTools\EventListener\Hook\InjectFormMessagesListener" autoconfigure="true">
1512
<argument type="service" id="request_stack"/>
1613
</service>
1714
<service id="Hofff\Contao\FormTools\EventListener\Hook\AddScrollToScriptListener" autoconfigure="true">

0 commit comments

Comments
 (0)