Skip to content

Commit 2d887b1

Browse files
feat: remove legacy code and cleanup
Release 1.1.0
1 parent 6358a59 commit 2d887b1

26 files changed

Lines changed: 170 additions & 286 deletions

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ indent_size = 2
6666

6767
# .htaccess
6868
[{_.htaccess,.htaccess}]
69-
indent_style = tab
69+
indent_style = tab

Classes/Hooks/FormRuntimeHooks.php

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
namespace LIA\LiaForm\Hooks;
1111

12-
use LIA\LiaForm\Services\TypoScriptReaderService;
13-
use TYPO3\CMS\Core\Site\Entity\Site;
14-
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
15-
use TYPO3\CMS\Form\Domain\Model\FormElements\Page;
1612
use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
1713
use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
1814

@@ -21,67 +17,31 @@
2117
*/
2218
class FormRuntimeHooks
2319
{
24-
/**
25-
* Hook the initializeCurrentPageFromRequest of the FormRuntime.
26-
*/
27-
public function afterInitializeCurrentPage(FormRuntime $formRuntime, ?Page $currentPage, ?Page $lastDisplayedPage = null, ?array $arguments = []): ?Page
28-
{
29-
// if ajax is disabled for this extension the hook returns the given value.
30-
if (!TypoScriptReaderService::isAjaxActive()) {
31-
return $currentPage;
32-
}
33-
34-
// Check if the form is submitted and if it was submitted by ajax.
35-
if ($formRuntime->getFormState()->isFormSubmitted() && $this->comparePageType($formRuntime->getRequest())) {
36-
// return null to invoke the finisher on ajax.
37-
return null;
38-
}
39-
40-
// check if currentPage is null and return lastDisplayedPage
41-
if (!$currentPage instanceof Page && !$formRuntime->getFormState()->isFormSubmitted()) {
42-
return $lastDisplayedPage;
43-
}
44-
45-
// Return currentPage otherwise form will not work.
46-
return $currentPage;
47-
}
48-
4920
/**
5021
* This hook is used to modify form values.
5122
*/
5223
public function afterSubmit(FormRuntime $formRuntime, RenderableInterface $renderable, $elementValue, array $requestArguments = [])
5324
{
54-
if ($renderable->getType() === 'PhoneAndAreaCode') {
55-
$areaCode = $formRuntime->getRequest()->getParsedBody()['tx_form_formframework'][$renderable->getIdentifier() . '-areaCode'];
56-
$elementValue = $areaCode . ' ' . $elementValue;
25+
if ($renderable->getType() !== 'PhoneAndAreaCode') {
26+
return $elementValue;
5727
}
5828

59-
return $elementValue;
60-
}
61-
62-
/**
63-
* Compare pageType integer from request and configuration.
64-
*/
65-
private function comparePageType(RequestInterface $request): bool
66-
{
67-
$routing = $request->getAttribute('routing');
68-
$site = $request->getAttribute('site');
29+
$parsedBody = $formRuntime->getRequest()->getParsedBody();
30+
$areaCodeIdentifier = $renderable->getIdentifier() . '-areaCode';
6931

70-
return (int)$routing->getPageType() === $this->getPageTypeFromConfiguration($site);
71-
}
32+
// Validate and sanitize area code input to prevent injection attacks
33+
$rawAreaCode = '';
34+
if (is_array($parsedBody) && isset($parsedBody['tx_form_formframework'][$areaCodeIdentifier])) {
35+
$rawAreaCode = $parsedBody['tx_form_formframework'][$areaCodeIdentifier];
36+
}
7237

73-
/**
74-
* Extract the page type for get-content/ from site configuration.
75-
*/
76-
private function getPageTypeFromConfiguration(Site $site): int
77-
{
78-
$fallback = TypoScriptReaderService::getContentPageTypeFallback();
79-
$pageType = $site->getConfiguration()['routeEnhancers']['PageTypeSuffix']['map']['get-content/'];
38+
// Limit length to prevent abuse
39+
$areaCode = substr(preg_replace('/[^0-9+\-() ]/', '', (string)$rawAreaCode), 0, 20);
8040

81-
if (empty($pageType)) {
82-
return $fallback ?? 0;
41+
if ($areaCode === '') {
42+
return $elementValue;
8343
}
8444

85-
return $pageType;
45+
return $areaCode . ' ' . $elementValue;
8646
}
8747
}

Classes/Services/TypoScriptReaderService.php

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

Classes/XClass/FormRuntime.php

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

Documentation/Configuration/Configuration.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
:navigation-title: Configuration
2+
23
.. _configuration:
34

45
=============
56
Configuration
67
=============
78

8-
.. attention::
9+
.. attention::
910
First load the typoscript of this extension in your static template.
1011

1112
This extension provides also an :ref:`example configuration <exampleConfig>`.
1213

1314

14-
.. contents::
15+
.. contents::
1516
:local:
1617
:depth: 1
1718

@@ -23,7 +24,7 @@ To override the default typoscript and yaml configuration create a `Setup.typosc
2324
Now copy this snippet in this file and adjust the path to your `CustomFormSetup.yaml` if you already have on otherwise create it in the set path and adjust the
2425
extension name in this path.
2526

26-
.. code-block:: typoscript
27+
.. code-block:: typoscript
2728
:caption: EXT:my_extension/Configuration/TypoScript/Extensions/LiaForm
2829
2930
@import 'EXT:form/Configuration/TypoScript/'
@@ -71,8 +72,9 @@ Yaml Configuration
7172
If you do not have a `CustomFormSetup.yaml` then create it in the path of the settings and past the following snippet.
7273
Here you have to adjust the path to your extension.
7374

74-
.. code-block:: yaml
75+
.. code-block:: yaml
7576
:caption: EXT:my_extension/Configuration/Yaml/CustomFormSetup.yaml
77+
7678
imports:
7779
- { resource: "./Form/Elements.yaml" }
7880

Documentation/Configuration/ExampleConfig.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _exampleConfig:
1+
.. _exampleConfig:
22

33
=====================
44
Example configuration
@@ -32,7 +32,7 @@ in the same directory where you pasted the CustomFormSetup.yaml in.
3232

3333
If you do not need them you can delete this part of code out this file.
3434

35-
.. code-block:: yaml
35+
.. code-block:: yaml
3636
:caption: Prototype registration
3737
3838
formManager:

Documentation/EventListener/Index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _EventListener
1+
.. _EventListener:
22

33
=============
44
EventListener
@@ -7,7 +7,7 @@ EventListener
77
This extension listen to some events to modify the core functionality.
88
Here you see a list of EventListeners that are subscribed by this extension.
99

10-
.. contents::
10+
.. contents::
1111
:local:
1212
:depth: 1
1313

@@ -18,8 +18,8 @@ AfterFlexFormDataStructureParsedEvent
1818
This event extends the default FlexForm of the core with a custom css field.
1919
Here you can set a custom css class on the current form. Please register your
2020
EventListener if you have also extend this FlexForm. This Subscriber
21-
is registered by the `lia-form/flex-form-extension`. You can aline your EventListener
22-
after this event by adding the following tag `after: 'lia-form/flex-form-extension'`
21+
is registered by the `lia-form/flex-form-extension`. You can align your EventListener
22+
after this event by adding the following tag `after: 'lia-form/flex-form-extension'`
2323

2424

2525

Documentation/Events/AllInOneEventListenerClass.rst

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. _allInOneEventListenerClass:
1+
.. _allInOneEventListenerClass:
22

33
==============================
44
All in one EventListener Class
@@ -7,13 +7,13 @@ All in one EventListener Class
77
You can also listen to all events in one class. Here you have to use your own functions.
88
In this example you see a class containing all Events at once.
99

10-
.. code-block:: php
11-
:caption: EXT:my_extension/Classes/EventListeners/LiaFormEventListeners.php
10+
.. code-block:: php
11+
:caption: EXT:my_extension/Classes/EventListener/LiaFormEventListeners.php
1212
1313
<?php
1414
declare(strict_types=1);
1515
16-
namespace MY\MyExtension\EventListener\Finisher;
16+
namespace MY\MyExtension\EventListener;
1717
1818
use LIA\LiaForm\Event\ApplyCustomSettingsToViewEvent;
1919
use LIA\LiaForm\Event\BeforeFormDefinitionCreatesEvent;
@@ -29,9 +29,20 @@ In this example you see a class containing all Events at once.
2929
*/
3030
public function finisherSetDefaultValueEventListener(SetDefaultValueEvent $event): void
3131
{
32-
// do some crazy stuff ...
32+
$formRuntime = $event->getFormRuntime();
33+
$formState = $formRuntime->getFormState();
34+
if ($formState === null) {
35+
return;
36+
}
3337
34-
$event->setFormRuntime($formRuntime);
38+
// The FormState is shared by all finishers of this submission, so this
39+
// default applies to every mail and cannot be scoped to one of them.
40+
// Compare against null and '' explicitly: empty() would also match a
41+
// legitimately submitted '0'.
42+
$salutation = $formRuntime->getElementValue('salutation');
43+
if ($salutation === null || $salutation === '') {
44+
$formState->setFormValue('salutation', 'Sir or Madam');
45+
}
3546
}
3647
3748
/**
@@ -42,9 +53,9 @@ In this example you see a class containing all Events at once.
4253
*/
4354
public function applyCustomSettingsToViewEventListener(ApplyCustomSettingsToViewEvent $event): void
4455
{
45-
// do some crazy stuff ...
46-
47-
$event->setEmailView($emailView);
56+
// The view is an object, so assigning to it is enough. Call
57+
// setEmailView() only to swap in a different FluidEmail instance.
58+
$event->getEmailView()->assign('myCustomVariable', 'someValue');
4859
}
4960
5061
/**
@@ -55,18 +66,22 @@ In this example you see a class containing all Events at once.
5566
*/
5667
public function beforeFormDefinitionCreatesEventListener(BeforeFormDefinitionCreatesEvent $event): void
5768
{
58-
// do some crazy stuff ...
69+
$formConfiguration = $event->getFormDefinitionConfigArray();
70+
71+
$formConfiguration['renderingOptions']['submitButtonLabel'] = 'Send';
5972
60-
$event->setFormDefinitionConfigArray($formconfiguration);
73+
// The configuration is an array, so it is copied on read: the setter
74+
// is what makes the change take effect.
75+
$event->setFormDefinitionConfigArray($formConfiguration);
6176
}
6277
}
6378
6479
Your event registration would look like this.
6580

66-
.. code-block:: yaml
81+
.. code-block:: yaml
6782
:caption: EXT:my_extension/Configuration/Services.yaml
6883
69-
MY\MyExtension\EventListener\Finisher\SetDefaultValueEventListener:
84+
MY\MyExtension\EventListener\LiaFormEventListeners:
7085
tags:
7186
- name: event.listener
7287
identifier: 'my-extension/finisher-set-default-values-event'

0 commit comments

Comments
 (0)