Introducing form API

Form API is one of the most important APIs when working with Drupal. It is used to build forms in a standardized and secure way, and will save you loads of time. This screencast shows an example implementation of form API, to build a text area and a submit button. In particular, this screencast includes:
  • Using the drupal_get_form function as callback for a menu item
  • Providing a form ID to drupal_get_form (and doing this with an array rather than a flat string)
  • Using the t() function, and why that is a good idea
  • The form API reference, found at http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html
code added/changed in wordlist.module /** * Implements hook_menu(). */ function wordlist_menu() { $items['admin/config/content/wordlist'] = array( 'title' => 'Word list', 'description' => 'Manage the list of globally available words on your site.', 'page callback' => 'drupal_get_form', 'page arguments' => array('wordlist_page'), 'access callback' => 'user_access', 'access arguments' => array('administer_site_configuration'), 'menu_name' => 'management', ); return $items; } /** * Builds the form for configuring Word list. */ function wordlist_page() { $form['wordlist_words'] = array( '#type' => 'textarea', '#title' => t('Words to inlcude in the list'), '#description' => t('The words included in this list will be available to Rules and other modules making use of the global "site" entity.'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save') ); return $form; }