Please notice: This article is for Drupal 6.
It would be great if we could add and remove fields to the contact form the same way we add them to content types using CCK, don't you think? But at this moment, this is not yet possible. As of today, there's no module that let us do it using the control panel. We need to do it "geek way", writing code.
The good news is that, as usual, we don't need to hack Drupal's core. We just use hook_form_alter to make the trick.
OK, let's do it.
As explained here, you need a place to put your custom code. A good place for your "code tricks" can be an empty module where you put all your custom stuff. You can go here for more info on creating an empty module.
For example, imagine you want to add a selector field to your site-wide contact form. For example, you want to ask user's favourite color. This would be the code:
function [--foo--]_form_contact_mail_page_alter(&$form, $form_state) {
// Options on our new selector box
$options = array(
'red' => t('Red'),
'green' => t('Green'),
'blue' => t('Blue'),
'none' => t('I don\'t bother')
);
$form['color'] = array(
'#title' => t('Favourite color'),
'#type' => 'select',
'#required' => true,
'#options' => $options,
'#default_value' => 'none'
);
// Resorting the whole form, so that our new field appears where we want to.
$order = array ('name', 'mail', 'subject',
'color', 'cid', 'message', 'copy', 'submit');
foreach($order as $key => $field) {
$form[$field]['#weight'] = $key;
}
}That's it, we modified the form. Remember to change [--foo--] by your custom module's name, and please notice we use the t() function to make the text of the new field translatable.
If you want more info on forms manipulation, check the Drupal Forms API quickstart guide.
Now, we must modify the output so we receive the data of this new field when someone sends us a message.
This is even easier. We use the mail_alter hook, being careful to alter only the contact form mail, as this hook is used for every mail the system sends:
function [--foo--]_mail_alter(&$message) {
// We have to be careful to alter only the contact form mail,
// as this hook is used for every mail the system sends.
if ($message['id'] == 'contact_page_mail') {
// We're gonna insert 'Favourite color' before message body.
$message['body'][2] = $message['body'][1];
$options = array(
'red' => t('Red'),
'green' => t('Green'),
'blue' => t('Blue'),
'none' => t('I don\'t bother')
);
$message['body'][1] = t('Favour').': '
.$options[$message['params']['color']];
}
}
Save and enjoy. You did it. You modified the site-wide contact form in Drupal. No big deal, isn't it?
This was the one I was looking,
Adding extrafileds and posting
great and thank you
Thanks for the tip, this is very useful. One note. When I first implemented this, the site had the Name field before the form instructions that should go at the top. I fixed this by numbering the keys in the order array starting at 1, eg:
$order = array (1 => 'name', 2 => 'mail', 3 => 'phone', 4 => 'subject', 5 => 'message', 6 => 'copy', 7 => 'submit');Excellent work, thanks!
Sorry, I'm not particularly good at php - would it be possible to give an example in doing this with a text field - for eg. An additional telephone number field...
This is great! Helped alot, good job.
Thanks very much, I've been looking for it and nobody had a clue. You saved my day.
Try http://drupal.org/project/contact_field module instead.
Thanks
Thank you for the post! I was looking for this a few days now. I just got a job to make .Net a Drupal and i couldn't find how you could a field! Now i know!
How can you add more fields this way, dont you think adding a code segment for each field inst a good way
How ever thanks for the idea
Dave
Hi, Dave.
You can add more than one field at once. Look for the first piece of code, locate $form['color']. This array is adding a row in our form. You just need to add more rows.
If you need more info on this subject, look for "drupal form API" in Drupal site. Also check this tutorial:
http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6
If you need the contact message to appear above the name field, try this:
$form[$field]['#weight'] = $key + 1;
That allows the existing "0" key to rest above the name field.
For that previous post when I say "contact message" I am referring to the default text that appears at the top of the form - not the message body field.
Hiya, thanks for share you have solved my problem.
I was looking for a long time to add a contact form to my site, thanks so much.