Form API

Export all user mail addresses to CSV



/**
 * Implements hook_menu().
 */
function export_mails_menu() {
  return array(
    'export_mails.csv' => array(
      'page callback' => '_export_mails',
      'access callback' => 'user_is_logged_in',
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function export_mails_block_info() {

  $blocks['export_mails'] = array(
    'info' => t('Export mails'),
  );

  return $blocks;

}

/**
 * Implements hook_block_view().
 */
function export_mails_block_view($delta) {
  switch ($delta) {
    case 'export_mails' :
      return array(

Create a form


/**
 * Implements hook_form().
 */
function MY_CUSTOM_FORM_form($node, &$form_state) {
  $form = array();
   
  $form['CUSTOM_FIELD'] = array(
    '#type' => 'textfield',
    '#label' => 'MY_LABEL',
  );
   
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  )
   
  return $form;
}
 
/**
 * MY_CUSTOM_FORM validation handler.
 */
function MY_CUSTOM_FORM_form_validate($node, &$form_state) {
   
}
 
/**
 * MY_CUSTOM_FORM submit handler.
 */
function MY_CUSTOM_FORM_form_submit($node, &$form_state) {
   
}

Handle multiple wrappers on an ajax callback

In this case the complete form is rebuild and the original messages array is replaced by the messages array related to the new form state. 

Redirect on a form submit

Redirect after a form submit, with or without querystring.

Keep form values after form submit

Sometimes form state values need to be kept to keep filter values on a form submit. In order to do this, the $form_state['rebuild'] boolean has to be set to true. This is done in the submit handler of the form.

Create a block with a form


/**
 * Implements hook_block_info().
 */
function MY_CUSTOM_MODULE_block_info() {
   
  $blocks['MY_CUSTOM_BLOCK'] = array(
    'info' => t('My custom block title as shown on admin/structure/block'),
  );

  return $blocks;

}

/**
 * Implements hook_block_view().
 */
function MY_CUSTOM_MODULE_block_view($delta) {

  switch ($delta) {
    case 'MY_CUSTOM_BLOCK' :
      return array(
      'subject' => t('MY_CUSTOM_BLOCK title.'),
      'content' => drupal_get_form('MY_CUSTOM_FORM'),
      );
      break;
  }
   
}

/**
 * Implements hook_form().
 */
Subscribe to RSS - Form API