Skip to main content

Working with Drupal forms

The Drupal Forms API provides developers with complete control over how forms work within Drupal. By using the Forms API even complex tasks such as working with AJAX are made simple. The Forms API varies a bit between major versions of Drupal but the principles remain the same, take a look at what we've blogged about Forms over the years below...

Read some of our articles about Drupal forms

Webform Protected Downloads

11th Mar 2024

I recently produced the first release of the Webform Protected Downloads module that is compatible with Drupal 10. It provides the ability for sites to have 'gated' content which users can download once they have filled out a form for their details. This can convert engaged visitors into leads, set up licenses for customers, or simply validate a user for access to a file. Put simply, as the project's description says, this module could be useful to you if:

  • You want to offer some files for download to either anonymous or registered users
  • You don't want those files to be...
Read more

A recipe for editing & translating over 100 fields

14th Jul 2020

I recently released a new contributed module to aid translation on Drupal 7 sites: Entity Translation: Separated Shared Elements Form (ETSSEF). Yes, it has a convoluted name! It finally resolves a suggestion from years ago in an Entity Translation project issue, to allow editing untranslatable fields separately to translatable ones. One of our clients has a multilingual product database site with a few hundred fields on their content, so anything like this that could reduce the size of their editing forms is useful. I figure the best way to demonstrate this is with a recipe that blends it together...

Read more

Including form values in an email

26th Jun 2018

Let's say you've built a custom form for your Drupal 8 site. It contains various elements for input (name, email address, a message, that kind of thing), and you want to send the submitted values in an email to someone (perhaps a site admin). That's a pretty common thing to need to do.

This could be done with Drupal's core contact forms, webforms, or similar -- but there are cases when a bespoke form is needed, for example, to allow some special business logic to be applied to its input or the form presentation. The drawback of a custom form...

Read more

Show elements with form #states when values do not match

17th Jan 2017

I've previously written about dynamic forms in Drupal, using #states to show or hide input boxes depending on other inputs. Since then, Drupal 7 and 8 have both got the ability to combine conditions with OR and XOR operators. This makes it possible to apply changes when a form element's value does not equal something, which is not obvious at all.

Read more

Multiple conditions for dynamic forms in Drupal 7

5th Jul 2011

Here's a quick follow-up to my original post on Dynamic forms in Drupal 7, as a reply to Wappie08, who asked me about combining conditions in the #states array to add increased control over the display of your form elements. The question:

Hi James Williams, I read your blog post about d7 & #states in the FAPI which is really cool! One problem is that the information is also listed in the example module, I was missing one important extra hint: how can you make an IF statement?

I mean:

IF field_1 is '1' or '2'...

Read more

Dynamic forms in Drupal 7

28th Jan 2011

When building forms, you will often want to only provide certain options if other options are chosen by a user. For example, there's no need to show the 'open link in new window' checkbox, if the 'make this into a link' checkbox hasn't been ticked. These kinds of dynamic forms haven't been easily available for Drupal... until now, with the #states for form elements in Drupal 7.

Take a peek at this example of dynamic forms in Drupal 7.

To make one form element be dependent on another element, you may have delved into AJAX, or tried using CTools...

Read more

Using CTools for form item dependency / visibility

James Silver
10th Jan 2011

What are CTools Dependencies?

One of several helpers included in the ctools module, ctools dependency is described on the module page itself as "a simple form widget to make form items appear and disappear based upon the selections in another item". It's designed to make it easy and quick to hide/show form elements based on the value of other form elements in the browser using javascript.

This article was written in reference to Drupal 6. See below for notes about Drupal 7.

How to add visual dependencies to your form elements

An example. This code can live in the form...

Read more

Quick Guide to using drupal_add_tabledrag and enjoying jquery drag and drop loveliness

6th Mar 2009

We are finding that the feature exciting most end users in Drupal 6 is the lovely new jquery based drag and drop, as seen on the blocks and menu edit pages - we will be quite happy never have to explain the concept of "weights" again. The best news is that you can add this functionality to your own forms for free - and here is how.

Build and theme the form

We are assuming your vaguely familiar with the form API - you can brush up here - so we won't go into too much detail here. Essentially...

Read more

Drupal 6 - Multiple instances of the same form on one page

10th Feb 2009

There's an excellent article over on gtrlabs: Drupal 5: How to process multiple instances of the same form on the same page that describes in detail how to have multiple copies of the same form on the same page. There are some subtle differences to use this technique in Drupal 6, which I'll explain below.

The idea of this technique is simple, drupal forms are identified by their 'form_id' and these ids must be unique on a page, so if you want the same form to appear more than once, you need to change the 'form_id' somehow. We do this...

Read more