From 4ab82c714dc670fee628085f08f41720256fcc88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Tue, 7 Sep 2021 14:35:35 +0200 Subject: [PATCH 1/2] WIP Implement referrer form finisher --- .../Classes/Form/AddReferrerFinisher.php | 29 +++++++++++ .../TypoScript/Extensions/Form.typoscript | 9 ++-- .../Yaml/FormFinishersBackend.yaml | 51 +++++++++++++++++++ .../Yaml/FormFinishersFrontend.yaml | 19 +++++++ .../{Forms => Yaml}/FormSetup.yaml | 0 .../Configuration/Yaml/FormsBase.yaml | 23 +++++++++ 6 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php create mode 100644 public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersBackend.yaml create mode 100644 public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersFrontend.yaml rename public/typo3conf/ext/ep_theme/Configuration/{Forms => Yaml}/FormSetup.yaml (100%) create mode 100644 public/typo3conf/ext/ep_theme/Configuration/Yaml/FormsBase.yaml diff --git a/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php b/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php new file mode 100644 index 00000000..f75b83dd --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php @@ -0,0 +1,29 @@ +finisherContext + ->getFormRuntime() + ->getFormDefinition() + ->getPageByIndex(0) + ->createElement('referrer', 'Text'); + + $field->setDefaultValue($referrer); + $field->setDataType('string'); + $field->setLabel('Referrer'); + + $this->finisherContext->getFinisherVariableProvider()->add( + $this->shortFinisherIdentifier, + 'referrer', + $referrer + ); + } +} diff --git a/public/typo3conf/ext/ep_theme/Configuration/TypoScript/Extensions/Form.typoscript b/public/typo3conf/ext/ep_theme/Configuration/TypoScript/Extensions/Form.typoscript index 190f7e6a..f0105ae1 100644 --- a/public/typo3conf/ext/ep_theme/Configuration/TypoScript/Extensions/Form.typoscript +++ b/public/typo3conf/ext/ep_theme/Configuration/TypoScript/Extensions/Form.typoscript @@ -1,13 +1,16 @@ -@import "EXT:form/Configuration/TypoScript/setup.typoscript" +@import 'EXT:form/Configuration/TypoScript/setup.typoscript' +@import 'EXT:form_element_linked_checkbox/Configuration/TypoScript/setup.typoscript' plugin.tx_form.view { templateRootPaths.10 = EXT:ep_theme/Resources/Private/Templates/Form/ } plugin.tx_form.settings.yamlConfigurations { - 100 = EXT:ep_theme/Configuration/Forms/FormSetup.yaml + 1630994710 = EXT:ep_theme/Configuration/Yaml/FormsBase.yaml + 1630994711 = EXT:ep_theme/Configuration/Yaml/FormFinishersFrontend.yaml } module.tx_form.settings.yamlConfigurations { - 100 = EXT:ep_theme/Configuration/Forms/FormSetup.yaml + 1630994710 = EXT:ep_theme/Configuration/Yaml/FormsBase.yaml + 1630994711 = EXT:ep_theme/Configuration/Yaml/FormFinishersBackend.yaml } diff --git a/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersBackend.yaml b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersBackend.yaml new file mode 100644 index 00000000..f155147c --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersBackend.yaml @@ -0,0 +1,51 @@ +TYPO3: + CMS: + Form: + prototypes: + standard: + formElementsDefinition: + Form: + formEditor: + editors: + 900: + selectOptions: + 1000: + value: 'BpnApiFinisher' + label: 'BusPro Finisher' + 1100: + value: 'AddReferrerFinisher' + label: 'Referrer Finisher' + propertyCollections: + finishers: + 1000: + identifier: 'BpnApiFinisher' + editors: + __inheritances: + 10: 'TYPO3.CMS.Form.mixins.formElementMixins.BaseCollectionEditorsMixin' + 100: + label: 'BusPro Finisher' + 110: + identifier: 'mapping' + templateName: 'Inspector-TextEditor' + label: 'Mapping' + propertyPath: 'options.mapping' + propertyValidators: + 10: 'NotEmpty' + 1100: + identifier: 'AddReferrerFinisher' + editors: + __inheritances: + 10: 'TYPO3.CMS.Form.mixins.formElementMixins.BaseCollectionEditorsMixin' + 100: + label: 'Referrer Finisher' + + finishersDefinition: + BpnApiFinisher: + formEditor: + iconIdentifier: 'form-finisher' + predefinedDefaults: + options: + mapping: 'lastName:text-1,firstName:text-2,gender:singleselect-1,email:email-1,street:text-3,zipCode:text-4,city:text-5,phone:telephone-1' + AddReferrerFinisher: + formEditor: + iconIdentifier: 'form-finisher' diff --git a/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersFrontend.yaml b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersFrontend.yaml new file mode 100644 index 00000000..b9e8cb73 --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormFinishersFrontend.yaml @@ -0,0 +1,19 @@ +TYPO3: + CMS: + Form: + prototypes: + standard: + finishersDefinition: + BpnApiFinisher: + implementationClassName: 'EP\EpTheme\Form\BpnApiFinisher' + formEditor: + label: 'BusPro Finisher' + options: + identifier: 'BpnApiFinisher' + mapping: 'lastName:text-1,firstName:text-2,gender:singleselect-1,email:email-1,street:text-3,zipCode:text-4,city:text-5,phone:telephone-1' + AddReferrerFinisher: + implementationClassName: 'EP\EpTheme\Form\AddReferrerFinisher' + formEditor: + label: 'Referrer Finisher' + options: + identifier: 'AddReferrerFinisher' diff --git a/public/typo3conf/ext/ep_theme/Configuration/Forms/FormSetup.yaml b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormSetup.yaml similarity index 100% rename from public/typo3conf/ext/ep_theme/Configuration/Forms/FormSetup.yaml rename to public/typo3conf/ext/ep_theme/Configuration/Yaml/FormSetup.yaml diff --git a/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormsBase.yaml b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormsBase.yaml new file mode 100644 index 00000000..7687bbb7 --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormsBase.yaml @@ -0,0 +1,23 @@ +TYPO3: + CMS: + Form: + persistenceManager: + allowedExtensionPaths: + 10: EXT:ep_theme/Resources/Private/Forms/ + prototypes: + standard: + formElementsDefinition: + Form: + renderingOptions: + templateRootPaths: + 1505042810: 'EXT:ep_theme/Resources/Private/Templates/Form/' + partialRootPaths: + 1505042810: 'EXT:ep_theme/Resources/Private/Partials/Form/' + layoutRootPaths: + 1505042810: 'EXT:ep_theme/Resources/Private/Layouts/Form/' + contactform: + __inheritances: + 10: 'TYPO3.CMS.Form.prototypes.standard' + applicationform: + __inheritances: + 10: 'TYPO3.CMS.Form.prototypes.standard' From d3647320fb1d4a5f1621799698fd947e3352a4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Tue, 7 Sep 2021 14:37:27 +0200 Subject: [PATCH 2/2] =?UTF-8?q?Finalize=20referrer=20form=20finisher=20by?= =?UTF-8?q?=20adding=20request=20middleware=CF=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Classes/Form/AddReferrerFinisher.php | 13 ++++++- .../Classes/Middleware/ReferrerMiddleware.php | 39 +++++++++++++++++++ .../Configuration/RequestMiddlewares.php | 12 ++++++ .../Configuration/Yaml/FormSetup.yaml | 29 -------------- 4 files changed, 63 insertions(+), 30 deletions(-) create mode 100644 public/typo3conf/ext/ep_theme/Classes/Middleware/ReferrerMiddleware.php create mode 100644 public/typo3conf/ext/ep_theme/Configuration/RequestMiddlewares.php delete mode 100644 public/typo3conf/ext/ep_theme/Configuration/Yaml/FormSetup.yaml diff --git a/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php b/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php index f75b83dd..6f5463bd 100644 --- a/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php +++ b/public/typo3conf/ext/ep_theme/Classes/Form/AddReferrerFinisher.php @@ -8,8 +8,14 @@ class AddReferrerFinisher extends AbstractFinisher { protected function executeInternal() { - $referrer = 'FooBarBaz'; + // Look for key 'referrer' in frontend session + $referrer = $GLOBALS['TSFE']->fe_user->getKey('ses', 'referrer'); + if (null === $referrer) { + return; + } + + // Add textfield to form containing referrer value $field = $this->finisherContext ->getFormRuntime() ->getFormDefinition() @@ -20,10 +26,15 @@ class AddReferrerFinisher extends AbstractFinisher $field->setDataType('string'); $field->setLabel('Referrer'); + // Add finisher variable for potential later use $this->finisherContext->getFinisherVariableProvider()->add( $this->shortFinisherIdentifier, 'referrer', $referrer ); + + // Remove session key + $GLOBALS['TSFE']->fe_user->setKey('ses', 'referrer', null); + $GLOBALS['TSFE']->fe_user->storeSessionData(); } } diff --git a/public/typo3conf/ext/ep_theme/Classes/Middleware/ReferrerMiddleware.php b/public/typo3conf/ext/ep_theme/Classes/Middleware/ReferrerMiddleware.php new file mode 100644 index 00000000..35eed7a5 --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Classes/Middleware/ReferrerMiddleware.php @@ -0,0 +1,39 @@ +getQueryParams(); + + if (!array_key_exists('ref', $queryParams)) { + return $handler->handle($request); + } + + $ref = $queryParams['ref']; + + /** @var SiteFinder $siteFinder */ + $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); + + try { + $site = $siteFinder->getSiteByPageId($ref); + $url = $site->getRouter()->generateUri($ref); + $GLOBALS['TSFE']->fe_user->setKey('ses', 'referrer', $url); + $GLOBALS['TSFE']->fe_user->storeSessionData(); + } + catch (SiteNotFoundException $exception) { + } + + return $handler->handle($request); + } +} diff --git a/public/typo3conf/ext/ep_theme/Configuration/RequestMiddlewares.php b/public/typo3conf/ext/ep_theme/Configuration/RequestMiddlewares.php new file mode 100644 index 00000000..ec4ed8b7 --- /dev/null +++ b/public/typo3conf/ext/ep_theme/Configuration/RequestMiddlewares.php @@ -0,0 +1,12 @@ + [ + 'ep/referrer' => [ + 'target' => \EP\EpTheme\Middleware\ReferrerMiddleware::class, + 'after' => [ + 'typo3/cms-frontend/site', + ], + ], + ], +]; diff --git a/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormSetup.yaml b/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormSetup.yaml deleted file mode 100644 index 255e9b4e..00000000 --- a/public/typo3conf/ext/ep_theme/Configuration/Yaml/FormSetup.yaml +++ /dev/null @@ -1,29 +0,0 @@ -TYPO3: - CMS: - Form: - persistenceManager: - allowedExtensionPaths: - 10: EXT:ep_theme/Resources/Private/Forms/ - - prototypes: - standard: - finishersDefinition: - BpnApiFinisher: - implementationClassName: 'EP\EpTheme\Form\BpnApiFinisher' - formElementsDefinition: - Form: - renderingOptions: - templateRootPaths: - 100: 'EXT:ep_theme/Resources/Private/Templates/Form/' - partialRootPaths: - 100: 'EXT:ep_theme/Resources/Private/Partials/Form/' - layoutRootPaths: - 100: 'EXT:ep_theme/Resources/Private/Layouts/Form/' - - contactform: - __inheritances: - 10: 'TYPO3.CMS.Form.prototypes.standard' - - applicationform: - __inheritances: - 10: 'TYPO3.CMS.Form.prototypes.standard'