(Tips) Overriding contact module, no core hacking required!
Tips : Overriding contact.module, no core hacking required!
-
First, it will help to tell you that I put this code in a custom client-specific module. I use this approach when developing client sites. I create a module with the client name and then put any customizations that are needed to forms, theme functions, etc. inside of it. It helps keep things better maintaned. If enough customizations are needed, I suppose I would then put each grouping into its own module, and keep them within the same module category.
The project I was working on required a contact form that contained the following things that are not provided by the core contact.module or any related contrib modules.
- a daytime contact number
- an evening contact number
Also, I had to remove the message body and subject that would normally appear.
Now, I had enough experience with hook_form_alter that I could modify the form to my heart's desire. The code for that is below:
<?php
function contactoverride_form_alter($form_id, &$form){
if($form_id == 'contact_mail_page'){
//this is the name of the form from contact.module
//add new fields/values
$form['day_phone'] = array(
'#type' => 'textfield',
'#title' => 'Daytime phone',
'#required' => TRUE,
'#weight' => 1,
);
$form['evening_phone'] = array(
'#type' => 'textfield',
'#title' => 'Evening phone',
'#required' => TRUE,
'#weight' => 2,
);
$form['agreement'] = array(
'#type' => 'checkbox',
'#title' => 'Yes, I would like to learn more.',
'#weight' => -29,
'#attributes' => array('class' => 'agreement'),
'#prefix' => '<div class="wideme">',
'#suffix' => '</div>',
);
//this element is added to help theme the form. completely optional.
$form['opening'] = array(
'#value' => '<div id="formopening">',
'#weight' => -30,
);
$form['closing'] = array(
'#value' => '</div>',
'#weight' => 40,
);
}
}
?>However, I had never overwritten the submit and validate functions for a *core* form before. I was worried that I might have to resort to hacking core.
[Read more..] - Courtesy : http://www.trevortwining.com
- guru's blog
- Login to post comments
![Drupal-6-Book-[Building Powerful and Robust Websites with Drupal 6].jpg](http://www.drupalranch.com/images/Drupal-6-Book-[Building%20Powerful%20and%20Robust%20Websites%20with%20Drupal%206].jpg)