[support] Confessions of a Drupal Hacker 1
J-P Stacey
jp.stacey at torchbox.com
Fri Nov 16 10:16:12 UTC 2007
Fred Jones wrote:
> My 20 lines of PHP code (mostly copied from other such pages) is indeed
> a lot easier. For me at least. :)
The usefulness of doing it the Drupal way comes from being able to hook into
existing workflows, and to a certain extent the hook methodology of the API
means development is sandboxed to just those hooks, and all the bits of your
site can communicate at a later date more easily. I'm assuming you're happy
creating an empty module:
1. If you want to be able to handle page requests to a certain URL, you
use the menu hook i.e. a function called mymodule_menu(). You don't need
this if you're hooking onto the existing contact form in some way.
2. If you want to hook into the existing workflow of a particular form,
then every form built properly has an ID (an advantage of you building your
forms properly). Using this, there are a number of points in the
creation/validation/action workflow you can hook into:
http://drupal.org/node/165104
By using a function called mymodule_form_alter() you can add the property
$form['#submit'] = 'mymodule_contactform_submit_whatever' to the chunk of
form API (an abstracted array representation of forms). That means that this
callback gets activated during the post-validation process.
3. In your custom submission function, one possibility is to just send
your own email and submit page content back to the user. This would mean
your module would look like the below.
Larry's extra suggestions are refinements on this: they mean you don't
necessarily have to assemble your own email completely; they also mean the
acknowledgement page is one step away from the POSTed details i.e.
refreshing it won't cause the email to be sent twice. There may be other
security issues I'm not aware of, but I certainly bow to the right and
proper way of doing things. But as a first attempt, something like:
<?php
function mymodule_form_alter($form_id, &$form){
if ($form_id == "contact_mail_page") {
$form['#submit'] = "mymodule_special_woot_submit";
}
}
function mymodule_special_woot_submit($form, $form_values) {
// Send the email
drupal_mail('mymodule_special_email',
$form_values['email'],
"Thank you for your submission to our site!",
"Here's what you submitted... blah blah blah"
);
// Write some content
$content = "<p>". t('Thank you! You submitted blah blah blah') . "</p>";
print theme('page', $content);
// Disable any further form processing
exit();
}
would probably do you well. I make that 20 lines, which I too have cut and
paste a bit from other sources!
Cheers,
J-P
--
J-P Stacey
+44 (0)1608 811870
http://torchbox.com
More information about the support
mailing list