4 steps form (multistep form) ctools

<!--?php
function ctools_multistep_menu() {
  $items['add/%ctools_js/multistep'] = array(
      'title' =--> 'Ctools 4 steps form',
      'page callback' => 'ctools_add_multistep',
      'page arguments' => array(1),
      'access callback' => TRUE,
      'type' => MENU_CALLBACK,
  );
  return $items;
}
 
function ctools_add_multistep($js = NULL, $step = NULL) {
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
  }
 
  $form_info = array(
    'id' => 'add_multistep', //form id
    'path' => "add/" . ($js ? 'ajax' : 'nojs') . "/multistep/%step", //pass the step we're on to the form, step1, step2, ..etc
    'show trail' => TRUE, //show the breadcrumb / path trail for the forms
    'show back' => TRUE, //show the back button
    'show cancel' => TRUE, //show the cancel button
    'show return' => FALSE, //show the update and return button
    'next callback' =>  'ctools_multistep_wizard_next', //a callback function to run when the next button is pressed
    'finish callback' => 'ctools_multistep_wizard_finish', //callback when finish button is pressed
    'cancel callback' => 'ctools_multistep_wizard_cancel', //callback when cancel button is pressed
    'order' => array( // this controls order, as well as form labels
      'step_1' => t('Basic Info'),
      'step_2' => t('File upload'),
      'step_3' => t('Step 3'),
      'step_4' => t('Step 4'),
    ),
    'forms' => array( // here we map a step to a form id.
      'step_1' => array( //What we're doing here is telling the wizard when step1 is passed as arg show the form with form_id ctools_multistepstep_1
        'form id' => 'ctools_multistep_step_1'
      ),
      'step_2' => array(
        'form id' => 'ctools_multistep_step_2'
      ),
      'step_3' => array(
        'form id' => 'ctools_multistep_step_3'
      ),
      'step_4' => array(
        'form id' => 'ctools_multistep_step_4'
      ),
    ),
  );
 
  $object_id = 'ctools_multistep';
 
  if (empty($step)) {
    ctools_multistep_cache_clear($object_id);
    $step = "step_1";
  }
 
 
  $object = ctools_multistep_cache_get($object_id);
 
  $form_state = array(
    'ajax' => $js,
    'object_id' => $object_id,
    'object' => &$object,
  );
 
  // Send this all off to our form. This is like drupal_get_form only wizardy.
  ctools_include('wizard');
  $form = ctools_wizard_multistep_form($form_info, $step, $form_state);
  $output = drupal_render($form);
 
  return $output;
}
function ctools_multistep_step_1_submit(&$form, &$form_state) {
  $form_state['object']->name = $form_state['values']['name'];
  $form_state['object']->dateitem = $form_state['values']['dateitem'];
  $form_state['object']->dateitem2 = $form_state['values']['dateitem2'];
  $form_state['object']->user = $form_state['values']['user'];
  $form_state['object']->gender = $form_state['values']['gender'];
  $form_state['object']->desc = $form_state['values']['desc'];
}
 
function ctools_multistep_step_2_submit($form, &$form_state) {
  $form_state['object']->marker_fid = $form_state['values']['marker_fid'];
}
 
function ctools_multistep_step_1($form, &$form_state) {
  $form_state['title'] = t('4 steps form');
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $form_state['object']->name,
    '#required' => TRUE,
  );
  $dateformat = 'm/d/Y';
  $d = new DateTime();
  $datevalue = $d->format($dateformat);
  $form['dod']['dateitem'] = array(
    '#type' => 'date_select',
    '#title' => t('Date1'),
    '#date_format'   => $dateformat,
    '#default_value' => $datevalue,
    '#default_value' => $form_state['object']->dateitem,
    '#date_label_position' => 'within',
    '#date_timezone' => date_default_timezone(),
    '#date_increment' => 1,
    '#date_year_range' => '-3:+3',
    '#value_callback' => 'date_select_element_value_callback',
    '#tree' => FALSE,
  );
 
  $form['dob']['dateitem2'] = array(
    '#type' => 'date_select',
    '#title' => t('Date2'),
    '#date_format'   => $dateformat,
    '#default_value' => $form_state['object']->dateitem2,
    '#date_label_position' => 'within',
    '#date_timezone' => date_default_timezone(),
    '#date_increment' => 1,
    '#date_year_range' => '-3:+3',
    '#value_callback' => 'date_select_element_value_callback',
    '#tree' => FALSE,
  );
 
  $form['user'] = array(
    '#type' => 'textfield',
    '#title' => t('Select taxonomy'),
    '#default_value' => $form_state['object']->user,
    '#required' => FALSE,
    '#autocomplete_path' => 'taxonomy/autocomplete/1',
  );
 
  $gender_arr = array();
  $gender_arr['Male'] = t('Male');
  $gender_arr['Female'] = t('Female');
  $form['gender'] = array(
    '#type' => 'select',
    '#title' => t('Gender'),
    '#default_value' => $form_state['object']->gender,
    '#required' => FALSE,
    '#options' => $gender_arr
  );
 
  $form['desc'] = array(
    '#type' => 'textarea',
    '#default_value' => $form_state['object']->desc,
    '#title' => t('Short "description" for the ctools'),
    '#required' => FALSE,
  ); 
 
 
 
  return $form;
}
 
function ctools_multistep_step_2($form, &$form_state) {
 
  $name = $form_state['object']->name;
  //$options = ctools_multistep_options();
  //$choice = $options[$form_state['object']['choices']];
 
  $form['marker_fid'] = array(
    '#title' => t('Image'),
    '#type' => 'managed_file',
    '#default_value' => $form_state['object']->marker_fid,
    '#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
    '#upload_location' => 'public://',
  );
  return $form;
}
 
function ctools_multistep_step_3($form, &$form_state) {
  return $form;
}
 
function ctools_multistep_step_4($form, &$form_state) {
  $form['story'] = array(
    '#type' => 'textarea',
    '#title' => t('Write Stories or Special Memories'),
    '#required' => FALSE,
  ); 
  return $form;
}
 
/**
 * Cache helpers
 */
function ctools_multistep_cache_set($id, $object) {
  ctools_include('object-cache');
  ctools_object_cache_set('ctools_multistep', $id, $object);
}
 
/**
 * Get the current object from the cache, or default.
 */
function ctools_multistep_cache_get($id) {
  ctools_include('object-cache');
  $object = ctools_object_cache_get('ctools_multistep', $id);
  if (!$object) {
    // Create a default object.
    $dateformat = 'm/d/Y';
    $d = new DateTime();
    $datevalue = $d->format($dateformat);
    $object = new stdClass;
    $object->name = '';
    $object->dateitem = $datevalue;
    $object->dateitem2 = $datevalue;
    $object->user = '';
    $object->gender = '';
    $object->desc = '';
    $object->marker_fid = '';
  }
  return $object;
}
/**
 * Clear the wizard cache.
 */
function ctools_multistep_cache_clear($id) {
  ctools_include('object-cache');
  ctools_object_cache_clear('ctools_multistep', $id);
}
 
/**
 * Next callback
 */
function ctools_multistep_wizard_next(&$form_state) {
  ctools_multistep_cache_set($form_state['object_id'], $form_state['object']);
}
/**
 * Finish callback
 */
function ctools_multistep_wizard_finish($form_state) {
  $form_state['complete'] = TRUE;
}
Help Share this Article