You are here

function flexiform_multistep_wrapper in Flexiform 7

Page callback for CTools Multistep Forms.

Parameters

$flexiform:

string $display:

mixed $base_entity_id:

string $step:

Return value

array The page array.

1 string reference to 'flexiform_multistep_wrapper'
flexiform_multistep_flexiform_wrapper_alter in flexiform_multistep/flexiform_multistep.module
Implements hook_flexiform_wrapper_alter().

File

flexiform_multistep/flexiform_multistep.pages.inc, line 18
Page callbacks for Flexiform Multistep.

Code

function flexiform_multistep_wrapper($flexiform, $base_entity, $js = FALSE, $current_step = NULL) {
  global $user;

  // Start setting up our ctools wizard multistep form info.
  ctools_include('wizard');
  ctools_include('object-cache');
  $menu_item = menu_get_item();
  $settings = isset($flexiform->settings['flexiform_multistep']) ? $flexiform->settings['flexiform_multistep'] : array();
  $form_info = array(
    'id' => 'flexiform_multistep_' . $flexiform->form,
    'path' => $menu_item['href'] . '/%step',
  );
  if (isset($settings['form_info'])) {
    $form_info += $settings['form_info'];
  }

  // Add in our pages from the existing field groups.
  $groups = field_group_info_groups('flexiform', $flexiform->form, 'form');
  foreach ($groups as $group) {
    if ($group->format_type == 'flexiform_step') {

      // Skip groups with no children.
      if (!count($group->children)) {
        continue;
      }
      $step = substr($group->group_name, 6);
      $form_info['forms'][$step] = array(
        'form id' => 'flexiform_multistep_step_form',
        'title' => $group->label,
        'weight' => $group->weight,
        'group' => $group,
      );
    }
  }
  uasort($form_info['forms'], 'drupal_sort_weight');
  foreach ($form_info['forms'] as $step => $info) {
    $form_info['order'][$step] = $info['title'];
  }
  $step_keys = array_keys($form_info['order']);
  $last_step = end($step_keys);
  $form_info['forms'][$last_step]['submit'] = 'flexiform_multistep_step_form_submit_redirect';

  // Cache important objects to maintain state between steps.
  $cache_key = $form_info['id'];
  if (!empty($base_entity->is_new)) {
    $entity_info = entity_get_info($flexiform->base_entity);
    $cache_key .= ':' . $base_entity->{$entity_info['entity keys']['id']};
  }
  $cache_key .= ':' . $user->uid;

  // Clear cache if were not on a step.
  if (empty($current_step)) {
    ctools_object_cache_clear('flexiform_multistep', $cache_key);
  }
  $object = ctools_object_cache_get('flexiform_multistep', $cache_key);
  if (empty($object['base_entity'])) {
    $object['base_entity'] = $base_entity;
  }

  // Build our form state with our arguments.
  $form_state = array(
    'flexiform_multistep' => TRUE,
    'build_info' => array(
      'args' => array(
        $flexiform,
        $object['base_entity'],
      ),
    ),
    'ajax' => !empty($js),
    'modal' => !empty($js),
    'last_step' => $last_step == $current_step,
  );
  form_load_include($form_state, 'inc', 'flexiform_multistep', 'flexiform_multistep.pages');
  $form_state['object_id'] = $cache_key;
  $form_state['object'] = $object;
  if (isset($object['flexiform_state'])) {
    $form_state['flexiform_state'] = $object['flexiform_state'];
  }
  $output = ctools_wizard_multistep_form($form_info, $current_step, $form_state);
  if ($js) {
    print $output;
    ajax_footer();
    drupal_exit();
  }
  else {
    return $output;
  }
}