Suppose we want to customize Joomla contact form. We want to do two modifications:
- Set some default text into text fields, e.g. to E-mail subject text box
- Set content of these fields via the URL argument (querry string), e.g. www.yoursite.com/book-excursion?subject=Book%20excursion
The purpose of this for me is to be able to display a customized contact form, which is displayed upon click on a button. The form can be shown through different buttons, e.g. “Book Makarska Excursion”, “Book Biokovo Excursion”. The form sends e-mail to web site owner, so, to be able to tell from which button is e-mail sent (i.e. what is being booked), we show different versions of the contact form depending on which button is clicked.
To do this in Joomla 3.4 we need to modify the file JOOMLA_ROOT\components\com_contact\views\contact\tmpl\default_form.php. But of course, to preserve our changes when Joomla is updated, we need to copy this file into our Joomla template. So even if we update Joomla later, the modified version stays untouched. After some research, I found that the correct location where to put the file is:
JOOMLA_ROOT\templates\YOUR_TEMPLATE\html\com_contact\contact\default_form.php
To meet requirement 2., we need to get url arguments in PHP. E.g. if we want to get value of the argument “subject”, we do that like this: $_GET[“subject”].
And to meet requirement 1. we need to pass this value to the form field. The e-mail subject text box in Joomla is created like this:
$this->form->getInput(‘contact_subject’);
In Joomla documentation (https://api.joomla.org/cms-3/classes/JForm.html), we can see that default value can be set via the 3rd argument to getInput(), so finally we do like this:
$this->form->getInput(‘contact_subject’, null, $_GET[“subject”]);
We can now access the form with predefined Subject field via the link. E.g. if the contact form is normally accessed via www.yoursite.com/book-excursion, to set Subject field to “Book excursion”, call it via the following link:
www.yoursite.com/book-excursion?subject=Book%20excursion
Now you might ask yourself how to access the contact form via such an URL. The answer is simple, just create an invisible menu, add a new item to it that opens this contact form, and set menu alias to book-excursion. Although the menu is not visible (as it is not assigned to a module), menu item will be accessible via the alias. To get rid of “index.php” in the URL you also need to enable Apache mod_rewrite module, but that’s another story.
We want to do a few additional customizations. We just want to show the contact form, without contact details. Here is a solution for that: http://www.joomla-css.nl/en/styling-joomla-3-content-components/contact-and-contactform. Citing this website: “If you only want to show a contact form, you hide all options of the Contact Options, except set the “Display Format” on Plain. Then you only see a contact form on the page” . “The headings “Contact” and “Contact Form” are unnecessary on the page. To hide these headings we add the following CSS to the template.css:”
div.contact h3 {
display: none;
}
That’s all!
Just imagine other beautiful stuff that can be done in similar way! 🙂 E.g. you can add one more URL argument “form_type” which will affect form shape, e.g. you can add some additional form fields like “Number of persons”.
The described method worked on Joomla 3.4, but should be identical (or very similar) for other Joomla versions. It should even work in Joomla 2.5 – I checked it and it has default_form.php file on the same location.