[Drupal] How to embed a webform in a page (Drupal 6)?

| | 2 min read

The webform module in Drupal gives you a great set of tools to let you create HTML forms with ease with a drag and drop interface for most of the commonly used HTML form elements. Since it is considered as another content type the forms are usually placed in a separate page of its own. Usually that is all that is required, but there are scenarios where you need to embed the webform in another content type like a page. Here is how to do it.

First you need to create a custom module to write the code to embed the form in a page or add it to an existing custom module.

We are going to use a hook - hook_preprocess_page() where hook should be replaced by the name of your module.
This is the hook we use in our module to embed the form.

Next we need to get the nid of the webform. Every content type in Drupal including a webform is considered to be a generic piece of content termed as a node. Every node has a unique node id commonly refered to as nid which is a number. You can easily obtain the nid using the following two methods.

1. Go the content list page and set the filter to webform and hover your mouse over the Edit link. The browser will display the url as something like node/ and is obviously the nid of the node.

2. Or you can use Firebug and view the source of the webform page. It is usually of the form webform_client_form_ where nid is the nid of the form in question.

After obtaining the nid of the form we should now proceed to write the code in hook_preprocess_page.

function mymodule_preprocess_page(&$vars)  {
  ..............................
    ..................

    $node = node_load(2721);
    $vars['apply_online_form'] = drupal_get_form('webform_client_form_2721',$node,array())
    ....................
..........................................
}

What we have done here is to load a the form into a template variable using node_load and drupal_get_form. The node_load() function loads all the required data of the content type associated with the nid passed to it as an argument. Once we have loaded the node we pass it as an argument along with the form id to drupal_get_form which as the name states retrieves the form in the node.

For the final step we need to print the variable in one of our template files possibly the page.tpl.php template file to get the webform embedded there.

Checkout our example of a webform embeddded in a page.