- Cashkeeperscanada.com revamp under way, date is now set at June 7
- The restaurant at the end of the universe
- Drupal 6 released. Drupalistas celebrate!
- Open source going mainstream
- Trevor Twining's Twitter Travels
- Overriding contact.module, no core hacking required!
- Google Docs sorts out document sharing clutter
- Second Outline Draft: Drupal Book
- Got my Drupal CVS account today
- outline draft for a drupal recipie book
TrevorTwining.com
Trevor Twining
phone: 905-228-9990 cel: 289-407-3957
email trevortwining _AT_ gmail.com
Recent blog posts
Recent comments
- trevortwining
Overriding contact.module, no core hacking required!
Wow, this was a really tough one. I learned how to totally redesign the Drupal contact form without hacking a single line of core.
Here's what we needed.
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.
Hacking core? NEVER!
Googling the problem and searching on drupal.org didn't provide much help.
A great resource was Pro Drupal Development by Matt Westgate and John VanDyk. It helped provide the insight into the fundamentals that I was missing. From that I figured what I needed to do to overwrite the forms.
The first step is to redirect the function that handles the validate and submit events. To do that, I needed a specific value to be set in my form array for each task. I accomplished this in my hook_form_alter() function. Note that this is clearer when shown in context with the full code sample below.
<?php
$form['#submit'] = array('contactoverride_form_submit' => array($extra_info));
?>Next, I needed to write the functions that did the validation and submission. In order to make them work *mostly* the same as how they do in contact.module, I copied and pasted their respective functions into mine. and then made the modifications I needed. You can see the completed functions below.
The submission function basically works the same as the core contact.module, but it modifies the message. My first instinct was to try and use hook_mail_alter() to accomplish this, but I quickly realized that I wouldn't have been able to work with anything inside the form submit if I took this approach.
After a bit of testing, I nailed down the bugs (most of them were related to the name of the submit function within the altered form) and got it finished.
Hope this helps anyone in the same situation.
Powered by Drupal

Comments
Post new comment