<<RAW>>
Summary
This module takes the values entered for pre-defined “source” fields and dynamically sets the values of predefined “target” fields via jQuery and AJAX. The “source” and “target” fields can be of any field type; i.e., they are not limited to drop down menu fields. There are two primary use cases for this module: Creating content and Editing content. For Example, there is a Drupal site that allows it users to create “Location” and “Field Trip” content:
Fig. 1 Location content

Fig. 2 Field Trip content

Creating Content Use Case
- Create a new location. (Fig. 1)
- Create a new Field Trip. (Fig. 2)
- Enter Title
- Select a date
- Select a location from the drop down menu.
- The Capital, Sunrise, and Sunset are populated with values based on the date and location selected.
- Submit.
- Extensions:
- The Editing Content Use Case (steps 2 - 7) is also applicable while creating content.
- If a value is not set for one of the source fields, then the target fields are not set.
Editing Content Use Case
- Edit an existing Field Trip
- Select a new date
- The Capital, Sunrise, and Sunset are populated with new values based on the new date.
- Select a new location
- The Capital, Sunrise, and Sunset are populated with new values based on the new location selected.
- Select a new date and a new location.
- The Capital, Sunrise, and Sunset are populated with new values based on the new date and new location selected.
- Save.
Technical Description
Technically speaking, these two use cases above represent a bidirectional communication between jQuery (client side) and Drupal (server side) via AJAX: $.get(url, params, callback); More specifically:
Fig. 3 Active Field Sequence Diagram

(1) The change event of the source field triggers an (2) AJAX call to a URL (i.e. “add/field-trip”), (3) which is mapped to a Drupal function via ActiveField_menu() hook that (4) calls the ActiveField_getTargets() function, which executes a SQL query to the Drupal database and (5) returns a JSON via drupal_to_js() function; (6) this JSON is then passed into anonymous function (i.e. the “call back”), which (7) sets the value of the target fields.
The params parameter of the AJAX $.get(url, params, callback) function is not specified in Fig. 3 above because its value depends on respective use case scenario being solved. For example, if the target fields are not dependent on the values of the selected source fields, then value is “null”; in this scenario, the target field will be populated with default values when the change events of the source fields are fired. In scenarios where the values of the target fields are in fact dependent on the values selected for the source fields, then params would look something like:
{sourcefield1:$(’#srcfld1-id’).val();,srfld2:$(’#sourcefield2-id’).val();}
In the Location/Field Trip scenario, params would look something like:
{date:$(’#edit-field-date-value’).val();, location:$(’#edit-field-location-key’).val();}
The exact selector ids may vary from system to system.
A Drupal module can define its own theme and forms or interoperate with an existing theme and forms. The ActiveField module is an example of the latter. In order to realize this, the .js file containing the JavaScript must be called from the ActiveField module itself and not from the theme layer (c.f.). One way to realize this is via hook_alter_form(). This hook utilizes a form id to alter a form. The form id of the Field Trip form is “field_trip_node_form”:
<input id=”edit-field-trip-node-form” type=”hidden” value=”field_trip_node_form” name=”form_id“/>
FireBug, can be used to obtain the field ids on the page:
- Go to the webpage.
- Open up FireBug.
- Click the HTML tab.
- Click the Ispect menu item.
- Move the mouse cursor over the fields (e.g. Capital, Sunrise, Sunset) and the respective HTML with the field id will be displayed to the console.
Code
The ActiveField module has the following files:
- ActiveField.info
- ActiveField.js
- ActiveField.module
A .install file was not created because no database tables need to be created or deleted when the module is activated or disabled; or at least, not at this time. Currently the “source” and “target” fields are pre-defined via code (i.e. hard-coded) and a separate instance of the module (i.e. copy/paste/modify) may be required for each set of “source” and “target” fields. In ActiveField 2.0, the follow features will be implemented:
- Pre-define “target” and “source” fields via Admin interface
- Create multiple sets of “target” and “source” fields
Software
- Drupal 5.3
- CCK Module 5.x-1.6-1
- Devel Module 5.x-0.4
- jQuery Interface Library 5.x-1.0
- jQuery Update 5.x-1.0
- JSCalendar 5.x-1.0
- FireFox
- FireBug
References
Drupal API
- drupal_ad_js()
- hook_alter_form()
- hook_menu()
- check_plain()
jQuery
- jQuery
- Learning jQuery
- Visual jQuery
Books
- Building Online Communities with Drupal, phpBB, and WordPress
- Drupal: Creating Blogs, Forums, Portals and Community Websites
- Drupal 5 Themes
- Pro Drupal Development
- Learning jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques
- jQuery in Action
Videos
- Lesson 12: jQuery Resurrected
- Lesson 07: An intro to jQuery magic
- Show login block using jQuery modal window
- Tutorials:Quick and Dirty Ajax (Screencast)
- Essentials of DOM and JavaScript
- Drupal School: Tasty JQuery Modules
- jQuery - Google Tech Talks
- AHAH! - Ajax + FormAPI = More Responsive UI
- jQuery AJAX forms
Blogs
- Leaning jQuery
- 15 Days of jQuery
Articles
- Javascript, jQuery and AJAX
- jQuery Hints and Snippets
- Drupal and Ajax working together
- Using jQuery
- How to pass a value in a form (jQuery) to Drupal (PHP)
- Using Ajax in Drupal 6
- Storing what jQuery returns in a PHP variable
- Tutorials: How to Get Anything You Want
- Simplify Ajax development with jQuery
- Modifying Forms in Drupal 5 and 6
- Adding JavaScript to your theme or module
- An Introduction to Unit Testing in Drupal
“Debugging”
- Drupal Module Development Support
- Drupal Dojo Discussion
- Debugging Drupal via XDebug & Eclipse PDT
- Quick-and-Dirty Debugging
- Drupal Podcast No. 31: Drupal Development Tools
- Methodology
- Client Side
- FireBug - use the ‘console’ tab to determine whether the AJAX callback is being invoked and to examine what is being returned
- FireBug - Click the “Profile” button. Execute the respective use case. Click the “Profile” button again. Review the resulting JavaScript function execution profile.
- FireFox - Delete the cache: Tools menu –> Clear Private Data…
- Server Side
- There are several options here.
- Quick Option: use drupal_set_message(”FunctionName: entered”);