I wanted a field on my site-wide contact form for the user's phone number. Here's the code I added to my module in hook_form_alter() to add it:

// Add phone number to contact mail page
  if ($form_id == 'contact_mail_page') {
    $form['phone'] = array(
      '#type' => 'textfield',
      '#title' => t('Your phone number'),
      '#maxlength' => 255,
      '#default_value' => $edit['phone'],
      '#required' => false,
    );
    $form['name']['#weight'] = 0;
    $form['mail']['#weight'] = 2;
    $form['phone']['#weight'] = 4;
    $form['subject']['#weight'] = 6;
    $form['message']['#weight'] = 8;
    $form['copy']['#weight'] = 9;
    $form['submit']['#weight'] = 10;
    // Add the phone number to the email
    if ($_POST['edit']['phone']) {
      $_POST['edit']['message'] = t('Phone number: ') . $_POST['edit']['phone'] . "\n\n" . $_POST['edit']['message'];
    };
  }

You can see my implementation on my yearbook website.

What this does is adds a field for phone number and on submission of the form, it puts the phone number into the top of the body field.

The only problem is that if the user's form doesn't validate (e.g. they leave out the subject field) then when the form comes back to them to complete the phone number will already have been added to the subject field, as well as being in the phone field. Hence when they submit again, you will have the phone number twice.

Correct me if I'm wrong, but I don't think there's any other way to do this - I can't use hook_nodeapi as the contact form is not a node. So the only hook I seem to be able to use to gain access to the form is hook_form_alter.

Comments

Just added a new module for creation / validation of phone field via CCK
check it out at : http://drupal.org/project/phone

It 's a beginning, can still be improve

------------
Thierry Guégan
Paris + Vancouver

------------
Thierry Guégan
Paris + Vancouver

Hey, your post gave me the idea to add a "subscribe to newsletter" checkbox in my site-wide contact form.

I added my own _submit function and put it together as a small module that I called newsletter checkbox.

Regards!

Alexis Bellido
Ventanazul: the first bilingual forum for web developers.

I have developed a web form as suggested and enabled capcha. But now that I've abandoned the Site wide contact form I've lost Gotcha. Is there anyway to tie it in to the new webform?

This is a planned update to Gotcha, but is awaiting the rewrite of Spam.

NancyDru (formerly Nancy W. until I got married to Drupal)

I added these lines into contact.module:

in the section "Site-wide contact page", added a phone field under the mail field so that it looks like:

$form['mail'] = array('#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $user->uid ? $user->mail : '',
'#required' => TRUE,
);
$form['phone'] = array('#type' => 'textfield',
'#title' => t('Phone'),
'#maxlength' => 20,
'#required' => TRUE,
);

and then to include it into the autogenerated e-mail body, I added one line to "prepare the body" section as:

$body = implode("\n\n", $message);
$body = $body . "\n\nPhone: " . $form_values['phone'];

Probably 75% of us have done that, but as soon as the next release comes along, it goes away again. I would recommend the Contact Forms module much more than a core hack.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

I agree that hacking core is not the way to go. I cannot though - yet- see how the Contact Forms module aids in adding a field for the phone number to the contact form. Can you explain?

John

I'm getting round to it.

It doesn't, by itself, add the field; you must still create a form. But it then allows that form to be used in place of the standard contact form. When I did this, I used the Webform module.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Yes; that makes sense. Thanks.

I'm getting round to it.

Dear another way,

I have installed drupal version 6 and used you hack above, only it seems that i had to modify the contact.pages.inc file to add the following:

$form['phone'] = array('#type' => 'textfield',
'#title' => t('Phone'),
'#maxlength' => 20,
'#required' => TRUE,
);

as for the $body = $body . "\n\nPhone: " . $form_values['phone']; part, i have no idea where thtis goes in the new version. Can you please help me with this

what about Name? first and last?

and what about doing this so people can easily extend it? perhaps day phone and night phone for example? or work and home phone?

or best time to call?

you get the idea...

thank guys!

I was able to do the first part, but the second half I can't figure out where it goes!

Thanks,
Alicia

Edit the "contact.pages.inc" in the "Site-wide contact page" section and approximately at the line 55 adding the following:

$form['phone'] = array('#type' => 'textfield',
'#title' => t('Phone number:'),
'#maxlength' => 20,
'#required' => TRUE,
);

Then go to the "contact.module" file in the "Implementation of hook_mail()" section and add the following:

$message['body'][] = t('Phone number:', NULL, $language->language);
$message['body'][] = $params['phone'];

You have to past this code in both the "page_copy" and "user_copy" sections, at line 173 and 193 approximately.

I hope this can be useful.

Never try to hack the core modules of drupal as you upgrade to next release it will go in vain.
The best module to create all these fields is to use webform module it provides a way to add many fields,
But if you want to use contact form that comes with drupal and wanted to add phone field then its better to write a module and do changes in hook_form_alter().

Work Hard to understand what you have to do b4 U start, You may not be able to develop every detail but the more U know the less risk you take.

Work Hard to understand what you have to do b4 U start, You may not be able to develop every detail but the more U know the less risk you take.
http://drupal.coolpage.biz

I continued to work on the original post by jakeg. (but now in Drupal 6)
I kept the first part of the hook_form_alter() but instead of changing the $_POST['edit']['message'] I implemented the hook_mail_alter() as follows:

if ($message['id'] == 'contact_page_mail'){
        $phone = $message['params']['phone'];
        $name = $message['params']['name'];
        if (!empty($phone)){
                $message['body'][2] = $message['body'][1];
                $message['body'][1] = 'Phone number of '.$name.': '.$phone;
        }
}

This inserts the phone-number in the mail between the first line (with the name of the sender) and the main message of the mail.
This works fine for me. (also if the first submit gives validation errors)

Hi Hans
Can you pls post the full config of your implementation and where i need to ammend the exisitng configuration.
Pls state which file i need to to modify
thanks

To implement the hooks hook_form_alter() and hook_mail_alter() you have to write a small module that you name 'xyz'.
In the file /sites/all/modules/xyz/xyz.module you implement both functions as follows.

<?php
function xyz_form_alter(&$form, $form_state, $form_id){
   here comes the code by jakeg (see first post on this page) without the last 'if'
}
function xyz_mail_alter(&$message){
   here comes the code I wrote in my post of July 8, 2009
}

Don't forget to create the file /sites/all/modules/xyz/xyz.info and to enable the module xyz.
More information on hooks you find in http://api.drupal.org/

That works as expected. I couldn't make it work using _mymodule_submit_form_alter()...

Use http://drupal.org/project/contact_field and let it does the dirty work!

I want to add phone field in the contact form so i found contact_field module. But installing this module i was able to add additional fields in contact form but unfortunately mail functionality of webform was not working due to this. so i just uninstall the contact_field module and now e-mail from webform is working fine.
Anyone can help me.