8

I have a Drupal site where I am using the sited-wide system Contact to allow visitors to send messages to the site owner. Once a visitor submits the form, the user is redirected to the home page and a sticky appears at the top reading:

Your message has been sent.

My question then, is it possible to change this message and add some HTML to the output? Ideally I would add some javascript to create a virtual page view for the Google Analytics account so that I can track it as a Goal Conversion.

I imagine this would be a hook_ function sitting in my template file, but I'm relatively new to this process.

3 Answers 3

6

Sure, just use hook_form_alter() and add a callback to $form['#submit']. The easiest way to find your form's id is inspecting it using firebug. This article has a lot more detailed info: http://www.lullabot.com/articles/modifying-forms-drupal-5-and-6.

In short:

  • Find the id for the contact form, "contact_mail_page" or something (notice how hifens turn to underscores)
  • In a new custom module implement either: yourmodule_form_alter($form_id, &$form, 'contact_mail_page') or yourmodule_form_contact_mail_page_alter($form_id, &$form)
  • Modify the contact form as you want using the $form variable passed by reference
3
  • thanks for your response. I've read through that article and I'm no closer to this solution. Do you have an example? That article shows how to write a hook for a module, but the Contact form is a system module so if I make the change their then a core update would blow that change away. Maybe I'm still missing something.
    – shanabus
    Mar 17, 2011 at 13:41
  • I've edited the first post to include more details
    – Alex Weber
    Mar 17, 2011 at 21:47
  • thanks for the update. I'm still not clear on how to implement your solution. So I create a new module and put the code where? As I mention above, I'm new enough to Drupal that I've never had to write a module. Maybe I should just use that webform module. Thanks again for any help.
    – shanabus
    Mar 23, 2011 at 20:16
1

The webform module might be more suited to your needs if it's just a general site wide form.

1
  • I meant to add... the webform module allows you to specify a redirect url which is used when the form is successfully submitted. This also means you can manage the success page easily as a regular content type. Hope this helps. James
    – user630
    Mar 18, 2011 at 10:34
0

@shanabus, you're understanding of "write a hook for a module" is incorrect. The main goal of creating a custom hook for any existing module including a system module is to over-ride the behaviour. This is how you customise Drupal to a specific sites needs. You'll not edit the existing code but rather add new code that uses the "hook" system to add new/replacement functionality.

Typically a custom hook function lives in a custom module created specifically for the site by the author of the site. Consider it a place to store your site specific customisations that you'll not be sharing with the greater Drupal community. A classic use would be a hook_form_alter as Alex suggests above.

Consider looking at the hook API documentation for more details: http://api.drupal.org/api/drupal/includes--module.inc/group/hooks. This is often a concept that new Drupal developers struggle with so don't be discouraged if it takes some extra effort to get the concept figured out. We've all been here :)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.