0

Using Drupal 7, I have a content type that uses the email field module(CCK) so that the page author can enter an email address for readers to mail them. With display suite module together with views contact form module, the saved page displays the site wide contact form. The emailed responses are delivered to the email address that was entered by the content author. That works great.

I wanted to add extra fields and managed to do this with the code below which I added to the template.php file but the code only works with the site wide contact form on example.com/contact

My question: How can I add those fields to the email field module's contact form as well?

This is the code I'm using to include the fields in the contact form display:

function MYTHEME_form_alter( &$form, &$form_state, $form_id ) {
if( $form_id == 'contact_site_form' ){
    $form['message']['#weight'] = 6;
    $form['copy']['#weight']    = 7;
    $form['phone'] = array(
        '#type' => 'textfield',
        '#title' => t('Phone'),
        '#required' => TRUE,
        '#weight' => 4
    );
    $form['province'] = array(
        '#type' => 'textfield',
        '#title' => t('Province'),
        '#required' => TRUE,
        '#weight' => 5
    );
}
}

This is the code I'm using to include the field responses in the email that gets delivered:

function MYTHEME_mail_alter(&$message) {
if ($message['id'] == 'contact_page_mail' ||
$message['id'] == 'contact_page_autoreply') {
$message['body'][2] = t('My Phone Number') . ': "' . $message['params'][phone'] . '"';
$message['body'][3] = t('My Province') . ': "' . $message['params']    ['province'] . '"';
}
}
0

You'll need to replace email_mail_page_form_submit with your own submit handler, because the Email module doesn't add extra form items to params automatically.

Firstly, copy the code from the function email_mail_page_form_submit in email.module and add phone and province to the params:

function MYMODULE_email_mail_page_form_submit($form, &$form_state) {
    // ...

    $params['object'] = $object;
    $params['subject'] = $form_state['values']['subject'];
    $params['name'] = $form_state['values']['name'];
    $params['message'] = $form_state['values']['message'];
    // Add your extra params
    $params['phone'] = $form_state['values']['phone'];
    $params['province'] = $form_state['values']['province'];

    // ...
}

Secondly, replace the submit handler on the form:

function MYMODULE_form_alter( &$form, &$form_state, $form_id ) {
    // contact form alterations
    // ...

    // email page form alterations
    if ($form_id == 'email_mail_page_form') {
        // add your phone and province fields
        // ...

        // override the email module's submit handler
        $form['submit']['#submit'] = array('MYTHEME_email_mail_page_form_submit');
    }
}

Then you can add them to the message as needed in HOOK_mail_alter.

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.