Discussion:
[TYPO3-english] How to hook TYPO3 Form submit
Chris Taylor
2017-10-25 07:08:54 UTC
Permalink
Hello, your support for a new typo3 developer would be greatly appreciated.

I need to hook into a TYPO3 form submit event and my question is: do I need to write a TYPO3 Extension to achieve this?

Background:
-----------------
I installed TYPO3 version 7.6.23. During the install process I picked the default 'distribution' and I now have the "TYPO3 CMS bootstrap package". When I go to the Installed Extensions screen I have the Form Extension installed (key=form).

I have a page with a form on it and I need to hook into that form submit event and do a HTTP POST to another site with some of the form data. Optionally I would like to prevent the form submit from happening, depending on the return value of my POST to the other site - so perhaps what I need in-fact is a 'before submit' event to hook.

Do I need to write an extension to achieve this? If so how can I get started? There are extension tutorials on typo3.org but they imply I need to build a full blown MVC extension, which seems way over the top for my requirements.

Thank you,

Chris
Mikel
2017-10-25 07:41:18 UTC
Permalink
Hi Chris,

the form framework supports custom postprocessors, which allows you to handle form handling after submit.

https://docs.typo3.org/typo3cms/extensions/form/7.6/Configuration/Postprocessors/Index.html <https://docs.typo3.org/typo3cms/extensions/form/7.6/Configuration/Postprocessors/Index.html>

It seems, that 8.7 supports a bit more options with its finisher API.
https://docs.typo3.org/typo3cms/extensions/form/8.7/ApiReference/Index.html#finisher-options <https://docs.typo3.org/typo3cms/extensions/form/8.7/ApiReference/Index.html#finisher-options>

Mikel
Florian Rival
2017-10-25 07:48:30 UTC
Permalink
This post might be inappropriate. Click to display it.
Chris Taylor
2017-10-25 15:50:00 UTC
Permalink
Thank you both for your very useful support.

I have found the form extension aftersubmit hook, which looks pretty much what I need.

I will build the directory structure as you suggest and test.

The redirect post processor may be very useful also.
Chris Taylor
2017-10-29 05:50:50 UTC
Permalink
I cannot get this to work.

My extension is made up of the two files below. My extension does not appear in the Extension manager list. When I try and zip my extension and import it I get an 'extension not available error'. If I submit a form, nothing happens (I am expecting my die function to run).

How can I get this working please?

typo3conf/ext/myext/ext_localconf.php

<?php

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/form']['afterSubmit'][time()] = \Chris\SomeSpace\FooHook::class;

typo3conf/ext/myext/Classes/Hook/FooHook.php

<?php

namespace Chris\SomeSpace;

class FooHook {

public function afterSubmit(\TYPO3\CMS\Form\Domain\Runtime\FormRuntime $formRuntime, \TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface $renderable, $elementValue, array $requestArguments = [])
{
die("this got called");
return $elementValue;
}
}
Mikel
2017-10-29 08:19:17 UTC
Permalink
There is missing the extension declaration file ext_emconf —> https://docs.typo3.org/typo3cms/CoreApiReference/ExtensionArchitecture/DeclarationFile/Index.html <https://docs.typo3.org/typo3cms/CoreApiReference/ExtensionArchitecture/DeclarationFile/Index.html>
Example content:
$EM_CONF[$_EXTKEY] = [
'title' => ‚Your ext title',
'description' => '',
'category' => 'templates',
'author' => ‚Your name',
'author_email' => ‚your mail',
'state' => 'alpha',
'internal' => '',
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 0,
'version' => '0.0.1',
'constraints' => [
'depends' => [
'typo3' => '8.7.0-8.7.99',
],
'conflicts' => [],
'suggests' => [],
],
];
Chris Taylor
2017-10-29 09:27:15 UTC
Permalink
Thank you, that makes sense.

I realise however that I have been looking at the version 9-dev documentation (my client is using version 7.6)

https://docs.typo3.org/typo3cms/extensions/form/ApiReference/Index.html#aftersubmit

When I switch to docs for version 7.6 I can see no api or hooks for the form extension. Would you agree? Does this mean that I wouldn't be able to hook into any sort of form submission?

A comment on stackoverflow suggests I use javascript instead?
Jan Bednarik
2017-10-30 22:06:06 UTC
Permalink
Hi,

I believe that no pre or post submit hooks won't help you here. You will
need some Javascript here. You will have to send request with the
partial data via AJAX, server will then use cUrl to POST to the remote
server and return result and based on the result you will then submit
the form or not. So, you need an eId script to process the AJAX request.

Jan
Post by Chris Taylor
Hello, your support for a new typo3 developer would be greatly appreciated.
I need to hook into a TYPO3 form submit event and my question is: do I
need to write a TYPO3 Extension to achieve this?
-----------------
I installed TYPO3 version 7.6.23. During the install process I picked
the default 'distribution' and I now have the "TYPO3 CMS bootstrap
package". When I go to the Installed Extensions screen I have the Form
Extension installed (key=form).
I have a page with a form on it and I need to hook into that form submit
event and do a HTTP POST to another site with some of the form data.
Optionally I would like to prevent the form submit from happening,
depending on the return value of my POST to the other site - so perhaps
what I need in-fact is a 'before submit' event to hook.
Do I need to write an extension to achieve this? If so how can I get
started? There are extension tutorials on typo3.org but they imply I
need to build a full blown MVC extension, which seems way over the top
for my requirements.
Thank you,
Chris
Chris Taylor
2017-10-31 15:58:46 UTC
Permalink
Thank you.

I have been looking at embedding javascript with typoscript, something like this:

page.includeJS

would this be the correct approach?

And do I still use an extension for my ajax handler function? If so this would imply I need to make url route which maps to a function in my extension?
Jan Bednarik
2017-11-01 18:26:06 UTC
Permalink
Please reply to specific post, so the context is not lost.

Anyway, includeJS can be used, or there's includeFooterJS too.

The routing extension will easily provide access to yours controller
action method under /api/extenstion/some/route a will initialize
required components.

Jan
Post by Chris Taylor
Thank you.
page.includeJS
would this be the correct approach?
And do I still use an extension for my ajax handler function?  If so
this would imply I need to make url route which maps to a function in my
extension?
Chris Taylor
2017-11-06 04:42:17 UTC
Permalink
Thank you Jan but I'm really struggling to get any of this working.

You mention "The Routing Extension". Is this something from the extension repository I can download? I couldn't see anything. Can you send a link to it please? If I could set up such a route for my ajax url, how do I protect that url from XSS and CSRF for example?

I tried setting up routing using the details here https://docs.typo3.org/typo3cms/InsideTypo3Reference/CoreArchitecture/Backend/Routing/Index.html with an (AjaxRoutes.php file) and with more information here https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/JavaScript/Ajax/Backend/Index.html I wrote an ajax function that used the javascript variable TYPO3.settings.ajaxUrls but this didn't exist. I got the impression I also need to load up a javascript lib somewhere in order for this to work?

I have opened a separate thread on this forum asking for a Typo3 frontend ajax example but no replies yet.
Loading...