Drupal 7 Form API

Modify and Delete Record Operations - Drupal 7 Multi-Step Form (FAPI)

This post is part of a series. We discuss how to build a custom module using Drupal 7's API.

In our previous posts "Drupal 7 Multi-Step Form (FAPI) with Table Report" and  "Making Our Report Sortable and Paged - Drupal 7 Multi-Step Form (FAPI)", we created a multi-step form that allows users to add new records. We display a synchronized (dependent) table report which is both sortable and page-able.

In this post, we add  "Modify" and "Delete" record operations. The following screen-shots display our new form and report.

new form layout

In order to accommodate the new record operations, we add a field set. The field set notifies the user which record operation is currently selected ("Add" is selected by default). The field set acts as a container for the record operation form. Our module dynamically changes the field set and form elements when users select a record operation (E.G. Add, Modify or Delete).

The following screen-shots display the new "Modify" and "Delete" form elements.

 new delete

The "Modify" operation works the same way as the "Add" operation. Both operations are divided into 2 steps (AKA multi-step).

Our module now supports three record operations. Thus our module requires additional source code instructions. It's a good practice to keep your internal design (source code structure) modular. Keep the size of individual methods as small as possible. Well organized source code allows the developer to identify logic flow. Well organized code supports changeability. a measurement of how easy is your code to change.

In that spirit, we divide the form definition (hook_form implementation) into helper methods.

Making Our Report Sortable and Paged - Drupal 7 Multi-Step Form (FAPI)

In this post we are going to replace the stub code detailed in the my previous (Drupal 7 Multi-Step Form (FAPI) with Table Report). We are going to store our data in a new database table. We'll update our report so that users can sort it and scroll through pages.

We'll add a new file listing for our database table install (and unistall cleanup). I'm using the name "proto" for our example module. In Drupal 7,  you don't need to explicitly install your modules schema. Here is our proto.install listing:

Drupal 7 Multi-Step Form (FAPI) with Table Report

In this post we are going to show how easy it is using Drupal 7's Form API (aka FAPI) to create a multi-step form with a simple html table report.

We'll also demonstrate the development technique of stubbing. In our case, we haven't decided how we want to store our data (for our final design). Therefore, we will use a simple Collection class and Drupal cache mechanism to perform a temporary yet simple stub.

The stub allows a developer to concentrate on one task (e.g. form/user interface), while another developer designs the full implementation (i.e. decides/codes a Drupal Field API instance, custom database tables, etc).

In our example we going to have users enter their name and a favorite color. The first step in our multi-step form is to ask the user to enter their name:

Dependent Lists and Ajax Form Submission

In this post we going to demo how simple it is to create dynamic forms with Drupal 7 Form API (aka FAPI).

Our form will have 2 select lists (aka drop down combo boxes). Our first list lets a user select a US State. Once a state has been selected, we dynamically render a "city" select list. The city select list only contain cities for the selected state.

We will include a submit button, however the form will be submitted asynchronously (aka via AJAX). 

Upon submission, we will asynchronously render a short message. The message tells the user which city they just submitted.

Syndicate content