How to generate contact form as block in Drupal

Submitted by hemant.gupta on Fri, 09/04/2015 - 12:31

Drupal comes with a core module known as contact. This module provides a site wide contact form to the visitors for communicating with you. This module will give you a page with a form where people can type in their name, email and message. after submitting an email is sent to the mail that you configured. It can be used to provide feedback, suggestion, ask for technical support etc. The contact form is available on the contact page of the site the url is like /contact. There are times when you want your visitors to directly send their messages from the page they are currently present on without going to contact page. For that it would be great if we can have a form inside a block that can be placed at any region in the theme.

So in this post I will share with you about various methods to show a form that people can fill in to contact you.

Contact forms block module

Just enable this module and you will have a block named "Contact form" and then you can easily show this block on your site like you control other blocks. This approach is best when you basically want to show the contact form in a block

Form block module

This module's working is similar to the contact forms block module. You can use this module when you want to show node creation forms also in the blocks. Just remember to adjust the permissions accordingly.

Using Webform module

You can use this module to create your own custom contact form and then you can show it in a block using the features provided by this module. This approach is best when you want to add or remove fields from a contact form and also wanted to show the form in a block. This is probably one of the most widely used module for this purpose because of the various features in offers.

Using custom code

All these methods listed till now uses modules to accomplish this task but there is also a way to do this by adding a small piece of code in a block.

The code to use is this


module_load_include('inc', 'contact', 'contact.pages');
echo drupal_render(drupal_get_form('contact_site_form'));
?>

Remember to wrap this code into php tags. The advantage of using this method is that it provides you more flexibility. This can also be included at the theme level, or in a node as long as you select an input format that supports PHP code. These are all the methods for showing the contact form in a block. You can use any method you like.

Try all these approaches and use the method that suits your requirement the best.