Last updated August 26, 2009. Created on November 16, 2008.
Edited by bekasu, HansBKK, GiorgosK. Log in to edit this page.

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

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

christok’s picture

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);

?>

mummybot’s picture

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);
}
?>
ikbenirene’s picture

I tried to follow your steps, and I think i did everything perfectly well, but it does not work!

Does anyone know what can be wrong?

I hoped the contactform would appear on my node-13:
http://www.mooiebeloftes.nl/nl/node/13

Any help would be very welcome!

UPDATE:
It does work however with just entering this code in the text-field of the page: (and setting the page to php)

<?php
 
require_once drupal_get_path('module', 'contact') .'/contact.pages.inc';
  print
drupal_get_form('contact_mail_page');
?>
dseeley’s picture

Great - just what I was looking for.

For D7:

<?php
 
require_once drupal_get_path('module', 'contact') .'/contact.pages.inc';
  print
drupal_render(drupal_get_form('contact_site_form'));
?>
alienzed’s picture

solved.

sunwin’s picture

require_once drupal_get_path('module', 'contact') .'/contact.module';

repair this when submit:

Fatal error: Call to undefined function contact_load() in path/contact.pages.inc on line 129

hello,drupal

gausarts’s picture

This is what I did with drupal 7 to be placed under theme_preprocess_node:

<?php
  
if (arg(0) == 'node' && arg(1) == 8 && arg(2) == FALSE && module_exists('contact')) {
    
module_load_include("inc", "contact", "contact.pages");
    
$vars['contact_form'] = render(drupal_get_form('contact_site_form'));
   }
?>

Adjust NID accordingly. You can print it anywhere inside node.tpl, maybe at node--page.tpl.php:

<?php if(!empty($contact_form)): print $contact_form; endif;?>

There is also a module for this:
http://drupal.org/project/contact_form_on_node

love, light n laughter

Oleg’s picture

You can also do it with Panels module. Both for a page and a block (using mini-panel).

LeviG’s picture

correct. It also worked for us.