How do I add fields to the standard Drupal contact form? Can this be done programmatically or is it a task for a module?
1 Answer
Yes, you can do that programmatically using the function hook_form_alter
in a custom module. Follow this for creating a custom module.
function MODULE_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'contact_personal_form') {
$form['new_field'] = array (
'#title' => t('New field'),
'#type' => 'textfield',
'description' => t('The description of the new field goes here...'),
'#required' => TRUE, // make the user to fill it compulsorily
);
}
}
Update: I forgot to mention that, the above code will just add the field to the form. The field is not added the mail you send. You should implement hook_mail_alter
and add the field to the body, by something like:
function MODULE_mail_alter(&$message) {
if($message['id'] == 'contact_user_mail') {
$message['body'][] = $message['params']['new_field']; //value of key 'new_field' same as in $form['new_field'] above in form alter
}
}
-
And the site wide contact form would be contact_site_form I guess. Commented Nov 20, 2012 at 11:07
-
1@Neograph734 - you can find out the
$form_id
of any form by printing it out in thehook_form_alter
, in this case just addecho $form_id;
before theif
condition above. Note that the$form_id
will be printed on the respective form.– AjitSCommented Nov 20, 2012 at 11:20