6

I want to add a phone field and a company field to the contact form. How to overwrite the form in template.php? Thank you

  • This field should be static and has only one phone and companu value? Or every user can put their own personal data? – khex Oct 27 '12 at 16:17
  • the user can put their own personal data – 250091017 Oct 28 '12 at 3:18
2

This module http://drupal.org/project/contact_field will do what you want and I have know about it since D6, but the last time I try it in D7 it was buggy.

So far the best alternative I have found that works all the time like a charm is the webform module http://drupal.org/project/webform as you will be able to add or remove fields as need it and the email support works well and you also get the bonus of storing all the contacts attempts in case your email server or service was down for any reason.

| improve this answer | |
  • thank you.if i want to do it with some code in template.php.how do i do? – 250091017 Oct 28 '12 at 3:19
1

Why not you use hook_form_alter() to add those two fields into the contact form?

function MYMODULE_form_alter( &$form, &$form_state, $form_id ) {
    if( $form_id == 'contact_site_form' ){
        $form['message']['#weight'] = 2;
        $form['copy']['#weight']    = 3;
        $form['phone'] = array(
            '#type' => 'textfield',
            '#title' => t('Phone'),
            '#required' => TRUE,
            '#weight' => 1
        );
        $form['company'] = array(
            '#type' => 'textfield',
            '#title' => t('Company'),
            '#required' => TRUE,
            '#weight' => 1
        );
    }
}

You can alter the contact mail by using hook_mail_alter(). The variable $message['params'] contains the form values submitted.

/**
 * Implementation of hook_mail_alter
 * Override mail structure
*/
function MYMODULE_mail_alter(&$message){
    if($message['id'] == 'contact_page_mail') {
        $language = $message['language'];
        $params   = $message['params'];
        /*
        $message['to'] = $more_receipient;
        $message['subject'] = $subject_overridden;
        $message['body'][0] = $msg_body;
        $message['body'][1] = $param_to_msg_body;
        */
    }   
}
| improve this answer | |
1

There are the following modules which you can consider:

  • Contact Forms

    Which expands the features of the site wide contact form. It eliminates the drop down category menu by generating a clean looking contact form.

  • Contact form fields

    However it's minimally maintained and it needs work for 7.x and it's not ready yet for the production. Please find my fork with some fixes, however still it doesn't work perfectly.


Alternatively this should be straight forward via Drupal API:

/**
 * Implements of hook_FORM_ID_form_alter().
 */
function foo_form_contact_site_form_alter(&$form, &$form_state, $form_id){
    $form['location'] = array(
        '#title' => t('Your location'),
        '#type' => 'textfield',
        '#required' => TRUE,
        );
        
    $order = array('name', 'mail', 'subject','location', 'cid', 'message', 'copy', 'actions');

    foreach($order as $key => $field) {
        $form[$field]['#weight'] = $key;
    }
}

/**
 * Implements of hook_mail_alter().
 * This is where the mail being sent if edited. I just add it the the end of the message
 */
function foo_mail_alter(&$message) {

    if ($message['id'] == 'contact_page_mail'){
        
        $message['body'][] .= t('This is their location - ').$message['params']['location'];
    }
}

Source: contact_add_fields.module at GitHub

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.