18

This seems strangely difficult to me. What I thought would just be a case of adding extra fields a bit like adding fields to Content Types. But no. I can't locate any facility to add say a phone number field to the Contact form? Anyone out there know?

migrated from stackoverflow.com Aug 1 '11 at 12:29

This question came from our site for professional and enthusiast programmers.

20

Expecting forms fields to be configured like content types is an over-generalization. Content types are for content (text, image, etc.) of the website, and more specifically, in Drupal, for nodes (the main piece of content in Drupal). Using the Field and Field UI module, one can easily adds fields to content type using a Web UI. Except for some specific cases, a form is not part of the content of a website but rather a way for users to add and edit information, sometimes content and sometimes not.

To customize (i.e. alter) a form in Drupal, the usual way is to create a custom module that implements the hook_form_FORM_ID_alter hook. This way, you can add form elements to existing forms provided by other modules. To use the information for fields added to the contact form this way, you will also need to alter the sent mail using an implementation of hook_mail_alter(). The following guide walks you through both steps in Drupal 7. (Note also this comment)

A non-code solution is to use the Webform module that provides a form content type and let you add forms as content to your website. These forms are configurable through a Web UI. Various processing steps can be configured for the information submitted through these forms. One of them is email sending. Using it for a custom contact form is a common use case. Webform comes with its own concepts and extension points. While it may prove to be a easier and faster solution for a one time simple forms, it may also reveal to be over-sized and complex to customize once a certain features threshold has been reached. The cost to add extra-features to a webform can then outperform the gain of avoiding writing a custom module in the first place.

1

It depends on what module you are using for your contact form. Some modules will provide a backend interface that requires no programming to add additional fields. A very popular module with this functionality is Webform.

Barring that, forms in Drupal can be altered by writing a custom module using the hook_form_alter hook. There are lots of tutorials out there on mdoule development and using this particular hook.

1

If your using core Contact module, hope the Contact form fields module might get finished soon for Drupal 7.

This module allows to add additional fields to site wide contact form like the profile module interface. Admin can define the fields and those will be available on site wide contact form. Please note that these fields will not be available to personal contact form for now as I don't see any specific needs.

If you want something now: 4 options:

  • Use a node type and transform it into a contact form.
  • Use a webform.
  • Build a custom module
  • Or even better: fork/help the contact_field module! The project page has just been updated, so it's at least moving again and would, ones finished, offer a very nice way of adding fields, just like on content types.

(adding this for search bot arena ;) )

0

Here is a sample code which shows how to add custom fields to the sitewide contact form via the forms api: new form fields example.

Enjoy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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