Login

Modify and Delete Record Operations - Drupal 7 Multi-Step Form - Source Lisitings

The blog post explaining the following source code is located here.

Listings for our form definition phase.:

function proto_menu() {
 
 $items['test/multi_step_form'] = array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array('multi_step_form'),
      'access arguments' => array('access content'),
      'title' => 'Multi Step Form Example',
      'description' => 'Multi Step Form Example',
      'type' => MENU_CALLBACK | MENU_NORMAL_ITEM,
  );
 
  $items['test/multi_step_form'] = array(
      'page callback' => 'drupal_get_form',
      'page arguments' => array('multi_step_form'),
      'access arguments' => array('access content'),
      'title' => 'Multi Step Form Example',
      'description' => 'Multi Step Form Example',
      'type' => MENU_CALLBACK | MENU_NORMAL_ITEM,
  );
 
  return $items;
}

Listings for our form definition phase.:

function multi_step_form($form, &$form_state, $operation = 'add', $folk_id = NULL) {
 
  $form['record_operation'] = array(
      '#type' => 'hidden',
      '#value' => $operation,
  );
 
  $form['report'] = array(
      '#type' => 'markup',
      '#markup' => drupal_render(folks_db_report()),
      '#weight' => 10,
  );
 
  if ($operation == 'modify' || $operation == 'delete') {
 
    if (!isset($form_state['input']['op'])) {
      $db = new DbFolk();
      $folk = $db->get($folk_id);
      $form_state['storage']['first_name'] = $folk->getFirst_name();
      $form_state['storage']['last_name'] = $folk->getLast_name();
      $form_state['storage']['color'] = $folk->getColor();
    }
    $form['id'] = array(
        '#type' => 'hidden',
        '#value' => $folk_id,
    );
  }
 
  switch ($operation) {
    case 'add' :
      $form['record_fieldset'] = array(
          '#type' => 'fieldset',
          '#weight' => 0,
          '#title' => t('Add New Person'),
          '#description' => t('Add a person by
          performing 2 steps. 1) Add the first and last name. 2) Choose the new
          person\'s favorite color.'),
      );
      $submit_value = 'Submit - Complete Form';
      $form = _populate_form_steps($form, $form_state, $submit_value);
      break;
    case 'modify' :
      $form['record_fieldset'] = array(
          '#type' => 'fieldset',
          '#weight' => 0,
          '#title' => t('Edit Record'),
      );
 
      $submit_value = t('Submit Change');
      $form = _populate_form_steps($form, $form_state, $submit_value);
      break;
 
    case 'delete' :
      $form['record_fieldset'] = array(
          '#type' => 'fieldset',
          '#weight' => 0,
          '#title' => t('Delete Record'),
      );
      $submit_value = t('Confirm Delete');
      $additonal_elements = _add_delete_op_elements($form_state, $operation, $folk_id, $submit_value);
      $form = array_merge_recursive($additonal_elements, $form);
      break;
  }
 
  return $form;
}
 
function _populate_form_steps($form, &$form_state, $submit_value = NULL) {
 
  if (empty($form_state['storage']['step'])) {
    $form_state['storage']['step'] = 0;
  }
 
  switch ($form_state['storage']['step']) {
    case 0:
      $form = _handler_step_0($form, $form_state, $submit_value);
      break;
    case 1:
      $form = _handler_step_1($form, $form_state, $submit_value);
      break;
  }
  return $form;
}
 
function _add_delete_op_elements(&$form_state, $operation, $folk_id, $submit_value) {
 
  $first_name = '';
  $last_name = '';
  $color = '';
 
  if (!isset($form_state['input']['op'])) {
    // stored during form request
    $first_name = $form_state['storage']['first_name'];
    $last_name = $form_state['storage']['last_name'];
    $color = $form_state['storage']['color'];
  }
  else {
    // get values from users submit
    $first_name = $form_state['imput']['first_name'];
    $last_name = $form_state['imput']['last_name'];
    $color = $form_state['imput']['color'];
  }
 
  $weight = 0;
 
  $form['record_fieldset']['first_name'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $first_name,
      '#prefix' => '<table><tr><td>',
      '#suffix' => '</td>',
      '#weight' => $weight++,
  );
 
  $form['record_fieldset']['last_name'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $last_name,
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#weight' => $weight++,
  );
 
  $form['record_fieldset']['color'] = array(
      '#type' => 'textfield',
      '#default_value' => $color,
      '#size' => 5,
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#weight' => $weight++,
  );
 
  $form['record_fieldset']['submit'] = array(
      '#type' => 'submit',
      '#value' => $submit_value,
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#weight' => $weight++,
  );
 
  $form['record_fieldset']['cancel'] = array(
      '#type' => 'submit',
      '#value' => 'Cancel',
      '#prefix' => '<td>',
      '#suffix' => '</td></tr></table>',
      '#weight' => $weight++,
  );
  return $form;
}
 
function _handler_step_0($form, &$form_state, $submit_value) {
 
  $weight = 0;
 
  switch ($submit_value) {
    case 'Submit - Complete Form' :
      $reset_cancel = 'Reset';
      break;
 
    case 'Submit Change':
      $reset_cancel = 'Cancel';
      break;
  }
 
  $form['color'] = array(
      '#type' => 'hidden',
      '#value' => isset($form_state['storage']['color']) 
                               ? $form_state['storage']['color'] : $form_state['input']['color'],
  );
 
  $form['record_fieldset']['first_name'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#maxlength' => 10,
      '#title' => t('First Name'),
      '#weight' => $weight++,
      '#prefix' => '<table border=0 cellspacing=3 celladding=3><tr><td>',
      '#suffix' => '</td>',
      '#default_value' => isset($form_state['storage']['first_name']) 
                                          ? $form_state['storage']['first_name'] : '',
  );
 
  $form['record_fieldset']['last_name'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#maxlength' => 10,
      '#title' => t('Last Name'),
      '#weight' => $weight++,
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#default_value' => isset($form_state['storage']['last_name']) 
                                          ? $form_state['storage']['last_name'] : '',
  );
 
  $form['record_fieldset']['reset'] = array(
      '#type' => 'submit',
      '#value' => $reset_cancel,
      '#weight' => $weight++,
      '#prefix' => '<td>',
      '#suffix' => '</td>',
  );
 
  $form['record_fieldset']['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Next Step',
      '#weight' => $weight++,
      '#prefix' => '<td>',
      '#suffix' => '</td></tr></table>',
  );
 
  return $form;
}
 
function _handler_step_1($form, &$form_state, $submit_value) {
 
  $weight = 0;
 
  $form['record_fieldset']['color'] = array(
      '#type' => 'select',
      '#options' => array('red' => 'red', 'white' => 'white', 'blue' => 'blue'),
      '#default_value' => isset($form_state['values']['color']) 
                                          ? $form_state['values']['color'] : 'red',
      '#title' => t('Favorite Color'),
      '#weight' => $weight++,
      '#prefix' => '<table border=0 cellspacing=2 celladding=2><tr><td>',
      '#suffix' => '</td>',
  );
 
  $form['record_fieldset']['submit'] = array(
      '#type' => 'submit',
      '#value' => $submit_value,
      '#weight' => $weight++,
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#title' => t('Click this button to complete the form and submit your entries.'),
  );
 
  $form['record_fieldset']['cancel'] = array(
      '#type' => 'submit', '#weight' => $weight++,
      '#value' => 'Previous Step',
      '#prefix' => '<td>',
      '#suffix' => '</td></tr></table>',
  );
 
  return $form;
}

Listings for our form submission phase.:

function multi_step_form_submit($form, &$form_state) {
 
  if (isset($form_state['storage']['step'])) {
    $form_state = _process_multi_step_submit($form, $form_state);
  }
  else {
    _process_delete_operation_submit($form, $form_state);
  }
}
 
function _process_delete_operation_submit($form, $form_state) {
 
  if ($form_state['values']['op'] == 'Confirm Delete') {
    $db = new DbFolk();
    $db->delete($form_state['values']['id']);
  }
  drupal_goto('test/multi_step_form');
}
 
function _process_multi_step_submit($form, $form_state) {
 
  switch ($form_state['storage']['step']) {
 
    case 0:
      switch ($form_state['values']['op']) {
        case 'Next Step' :
          if ($form_state['values']['record_operation'] == 'add') {
            $form_state['rebuild'] = TRUE;
            $form_state['storage']['first_name'] = $form_state['values']['first_name'];
            $form_state['storage']['last_name'] = $form_state['values']['last_name'];
            $form_state['storage']['record_operation'] = $form_state['values']['record_operation'];
          }
          if ($form_state['values']['record_operation'] == 'modify') {
            $form_state['rebuild'] = TRUE;
            $form_state['storage']['first_name'] = $form_state['values']['first_name'];
            $form_state['storage']['last_name'] = $form_state['values']['last_name'];
            $form_state['storage']['color'] = $form_state['values']['color'];
            $form_state['storage']['record_operation'] = $form_state['values']['record_operation'];
          }
          $form_state['storage']['step'] = 1;
          break;
        case 'Reset' :
        case 'Cancel' :
          unset($form_state['storage']);
          drupal_goto('test/multi_step_form');
          break;
      }
      break;
 
    case 1:
      switch ($form_state['values']['op']) {
        case 'Previous Step':
          $form_state['storage']['step'] = 0;
          $form_state['rebuild'] = TRUE;
          break;
        case 'Submit - Complete Form' :
          $db = new DbFolk();
          $db->add($form_state['storage']['first_name'], 
                         $form_state['storage']['last_name'], $form_state['values']['color']);
          drupal_goto('test/multi_step_form', array(
              'query' => array('sort' => 'desc', 'order' => 'Id')));
          break;
        case 'Submit Change' :
          $db = new DbFolk();
          $folk = new Folk();
          $folk->setColor($form_state['values']['color']);
          $folk->setFirst_name($form_state['storage']['first_name']);
          $folk->setLast_name($form_state['storage']['last_name']);
          $folk->setKey($form_state['values']['id']);
          $db->set($folk);
          drupal_goto('test/multi_step_form');
          break;
      }
      break;
  }
  return $form_state;
}