example_element.module

Version 1.4 (checked in on 2007/10/05 at 16:42:46 by drewish)

This is an example demonstrating how a module can define custom form elements.

Form elements are already familiar to anyone who uses Forms API. Examples of core form elements are 'textfield', 'checkbox' and 'fieldset'. Drupal utilizes hook_elements() to define these FAPI types, and this occurs in the core function system_elements().

Each form element has a #type value that determines how it's treated by the Form API and how it's ultimately rendered into HTML. hook_elements() allows modules to define new element types, and tell the Form API what default values they should automatically be populated with.

By implementing hook_elements in your own module, you can create custom form elements with their own properties, validation and theming.

In this example, we will define a phone number field that is expanded into several text fields for area code, phone number and extention, each of which is validated.

Functions

NameDescription
example_element_demo_formThis is a simple form to demonstrate how to use the phonenumber element we've defined.
example_element_elementsImplementation of hook_elements().
example_element_menuImplementation of hook_menu().
example_element_phonenumber_expandOur process callback to expand the control.
example_element_phonenumber_validateOur element's validation function.
example_element_themeImplementation of hook_theme().
theme_phonenumberTheme function to format the output.
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.