Embedding Contact form in a node

Why would you want to do this?

  • Contact page becomes a node, with all the benefits that implies
  • Easier to maintain and edit its content and permissions can be assigned

Step 1 of 3

Add in your theme's template.php the following code which allows node-specific theming

<?php
function phptemplate_preprocess_node(&$vars) {
   
$vars['template_files'][] = 'node-'. $vars['nid'];
    return
$vars;
}
?>

Clear the cache so the function can be noticed by the system since this is technically an override.

Note that Drupal maintains cached theming data through the theme registry. It must be cleared when setting up overrides. (read more about how theming works)

Step 2 of 3

Create a node which will serve as your contact page and note the node-id after creation. Lets assume its 38 in this example

Step 3 of 3

Create node-38.tpl.php in your theme's directory and add the following code in it

<?php
 
require_once drupal_get_path('module', 'contact') .'/contact.pages.inc';
 
 
//no need to maintain two version of node.tpl.php
 
include "node.tpl.php";

function
local_contact_page(){
   
$form = contact_mail_page();
   
// override default values here if you want
    // next one will select a different category
    //$form['cid']['#default_value'] = 0;
   
return($form);
}

function
local_contact_page_submit($form_id, $form_values){
    return(
contact_mail_page_submit($form_id, $form_values));
}

function
local_contact_page_validate($form, $form_state){
    return(
contact_mail_page_validate($form, $form_state));
}

print
drupal_get_form('local_contact_page');   
?>

NOTES

This is one way of doing this; if you need to modify the contact form itself, you can do so in the local_contact_page() but your best bet would be webform module.

If you know the changes needed for this to work in Drupal 5 please go ahead and edit this page but, one can probably figure it out by looking at the page that inspired me Quick Contact Block

Embed a Forward Form in a node.

chriskorzen - February 14, 2009 - 01:57

I've successfully modified this excellent snippet to allow embedding of a forward form in a node. This is useful if you want to encourage users to forward specific content. For instance, after signing a petition users can be redirected to a new page that asks them to send the petition to their friends and family.

To implement this, I used the following code in the node-x.tpl.php file (Step 3). Steps 1 and 2 remain the same.

<!-- Override links within the node body to hide the "Email this Page"
link provided on the parent node by the forward module.  Since we're
already displaying the forward form, this link is unnecessary and could
confuse users. -->

<style>

  .node .links {
     display:  none;
  }

</style>

<?php

 
//define the node we want to forward (replace x with the node id):
 
$nid = x;

 
//load the forward module
 
require_once drupal_get_path('module', 'forward') .'/forward.module';

 
//no need to maintain two version of node.tpl.php
 
include "node.tpl.php";


function
forward_page_local($nid){

   
$node = node_load($nid);
   
$path = 'node/'. $node->nid;
   
    return
drupal_get_form('forward_form', $path, $node->title);

}

print
forward_page_local($nid);

?>

Combining with contact_redirect

mummybot - May 28, 2009 - 09:10

If you use the module Contact Redirect (http://drupal.org/project/contact_redirect) you can get it to work with this solution by amending the function local_contact_page to the following:

<?php
function local_contact_page(){
   
$form = contact_mail_page();
   
// override default values here if you want
    // next one will select a different category
    //$form['cid']['#default_value'] = 0;

  
$form['#submit'][] = 'contact_redirect';

    return(
$form);
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.