forms api

Rendering the same for twice leads to incorrect submission

If you render the same form (with the same exact form id) on the same page more than once, Drupal will incorrectly submit all matching forms on the page to the first-rendered form. To avoid this, you must create dynamic form ids. This can be achieved by using hook_forms() to map dynamic form ids to the appropriate callbacks.

Here's an example from the usasearch_field module:

Building a Multistep Registration Form in Drupal 7

This article provides a step-by-step tutorial for creating a custom, multistep registration form via the Forms API in Drupal 7. For a Drupal 6 guide, I recommend Multistep registration form in Drupal 6.

Drupal 7's updated Form API makes the process of building multistep forms relatively painless. In combination with the excellent Examples for Developers module, it's really just a matter of copy, paste, and tweak.

We're going to be putting a slightly different spin on the standard approach to creating a multistep form.

Import form values from one form to another via AJAX

Drupal 7's new and improved Form API makes using AJAX a breeze. It enables you to add, replace, or remove form elements via AJAX without ever really having to get your hands dirty.

I recently had the opportunity to give it a whirl. Here's a the deal:
I have content type Resume and Profile2 type Job Seeker Profile. I'd like my users to be able to create up to 5 resumes, and I'd like them to have the option of importing information from their profile into their brand-spanking new resume. So, I'm going to create an 'Import from profile' button that will populate the resume fields with profile field values via AJAX.

Note: It is possible to accomplish something like this by setting default values via a token. However, in this case, I'd like to be able to import things like field collections, addresses, and fields with unlimited cardinality. Here's the code that accomplished it!

Drupal - Conditionally hiding CCK fieldgroups

Conditional Fields is a great Drupal module for conditionally hiding CCK fields.

Unfortunately, it can't hide CCK fieldgroups. It's also not ideal if you're concerned about security— it simply hides fields; it doesn't deny access. A recent project of mine required that I conditionally deny access to field groups, so I decided to implement a programmatic solution.

In this case, I needed to deny access to specific fieldgroups in content type 'company' based on:

  • The role of the acting user
  • The value of the field_company_type CCK field

The downside to this approach is that you must save and return to the node edit page before seeing a change in fieldgroup visibility.

Here's my solution:

Add address field to Signup form, save to Content Profile

Alright, so this a bit nuanced, but I thought that I'd share it anyway. Maybe this snippet will help someone somewhere.

Scenario

I'm using the Signup module to permit users to signup for events, but I'd like them to be able to enter more than the standard name and phone number. I'd like my users to be able to provide their address.

Beyond that, if the user has already provided their address on their user profile (generated using Content Profile and the Location CCK module), then I'd like the fields to prepopulate.

Furthermore (we're getting crazy here), if they enter a new address on the signup form, I'd like them to be able to save that address to their profile.

Whoa!

Actually, with the power of the Forms API, this becomes an easy task.