Jul 22, 2008

Howto: use Frontend data in a TYPO3 mail form

Today I had an interesting challenge. I had to create a contact form on the Intranet web site. This web site always has Frontend user logged in. I was going to use standard TYPO3 mailform. Typically such form contains fields for user name and e-mail. When I started to create the form, I immediately realized that it will be stupid to ask for this information if user is already logged it. I should find a way to insert this information automatically.

The obvious solution that come to the mind is to make an extension that will modify the form to include Frontend user data. Yes, it is possible.


But what I like about TYPO3 is that lots of things are possible without PHP programming. And adding Frontend user data to mail form is possible with a simple TypoScript. Here it is:

tt_content.mailform = COA_INT
tt_content.mailform.20.hiddenFields {
  name = TEXT
  name.data = TSFE:fe_user|user|username
  email = TEXT
  email.data = TSFE:fe_user|user|email
}


Let's start with line 2. It adds hidden fields to the form. The "hiddenFields" property is an array of content objects. Name of the object becomes name of the form field. Value of the object becomes value of the field. In lines 3 to 4 and 5 to 6 I created two fields. I used "data" stdWrap property to fetch Frontend user data. If you do not know what is stdWrap or "data", you can read about it in my article about stdWrap.

How does it look like in HTML? Here is a fragment:

<input type="hidden" name="fromname" value="test01" />
<input type="hidden" name="frommail" value="test@example.com" />


What about line 1? Why is it there?

The answer is simple: you need to make form non-cacheable to avoid caching of the first user who visits this page. You need unique user name and e-mail.

What if user is not logged in?

If user is not logged in, you can use TypoScript conditions to add normal fields to the form.

Extension or TypoScript?

After making this part of code I remembered something. Several times I saw that people make extensions when several lines of TypoScript can do the same work. It reminds me that the first obvious solution is not always the best. There should be no rush with implementation. Careful thinking always produces much better result.

Did I already pronounced this before? Oh, yes. It was mentioned twice: once in my "Background thinking" article and second time during my "Personal productivity" session in TYPO3 user group in the Netherlands in July 2008.

What do you think about it?

2 comments:

  1. Hi - first of all, I like the new blog :) I was worried it had gone offline permanently and that would have been a shame as it is the best Typo3 blog!

    Quick question - does this technique still work with Typo3 4.4.6? I have a mailform, and am following the instructions properly here. The hidden fields are appearing in the form, but the values are not pre-filling the input fields.

    The hidden fields in the form are being wrapped in a "div style="display:none;" (sorry wont let me post actual html here), so could that be causing the malfunction? (is there a way to stop Typo3 from physically hiding hidden form fields?)

    Kind Regards,

    Anthony

    ReplyDelete
  2. This is an old entry :) I am not sure if it will work with recent versions of TYPO3, especially after the integration of a new form component.

    Regarding the blog – no worries, it will not go offline. Some entries are still missing but I will restore them manually.

    ReplyDelete