I needed to add a checkbox to Drupal’s Contact Us form for a client of mine. The client requested that I add a way for those filling out the form to request a newsletter. I discovered it’s pretty easy to add a form field and get it added as part of the email that’s sent. You just need to hack into the module on your server (sorry, no GUI for this). Here’s what I did:
First, find the module file:
modules/contact/contact.module
If you prefer command line editing open up that file in your favorite editor. If you prefer the download/edit/upload approach via FTP that will work also. I assume if you have interest in this article, that you have some knowledge of either of those 2 methods. If not, I wonder how you set up Drupal in the first place.
Now that you are in edit mode, find a chunk of code that talks about not allowing anonymous users to send themselves a copy for spam reasons. Found that comment? Good, now just above it you are going to add your field. I found it easy to copy/paste an existing form element and edit it’s contents, but you can type it out if you like. Actually, feel free to copy/paste and edit my example below. Here’s what I ended up with:
$form['newsletter'] = array('#type' => 'checkbox', '#title' => t('Sign me up for the Newsletter'), ); |
You may want to add a comment somewhere in there to remind yourself at a later date that this is no longer part of the original configuration.
That was easy, now I just need to tell it to add content to the email sent out so that when that box is checked, it actually does something! Next look for a chunk of comment code that looks like this:
// Compose the body:
below that you will find a couple lines that start with “$message[] =” which is where the body of the email’s message gets created. Just add another line that says something like the following:
if ($form_values['newsletter'] == 1) { $message[] = "Yes, please send me a newsletter"; } else { $message[] = "No, please do not send me a newsletter"; } |
Now crank open your form and test it out. Assuming you are the recipient of the email being generated, you will get a nice extra piece of text at the bottom of the email with this new content based on the user’s input.