You are here

function _msnf_form_attach_buttons in Multistep Nodeform 7

Helper method to add the required buttons to a form.

Parameters

<array> $form: The form where the buttons will be attached to.

<array> $form_state: Current form state.

1 call to _msnf_form_attach_buttons()
msnf_form_node_form_alter in ./msnf.module
Implements hook_form_FORM_ID_alter().

File

./msnf.module, line 1150
Main functions for module "Multistep Nodeform".

Code

function _msnf_form_attach_buttons(&$form, &$form_state) {
  $steps = $form['#steps'];
  if (($current_step = _msnf_form_step_get_current($form, $form_state)) === FALSE) {

    // Step not found. Do nothing.
    return;
  }
  $step_settings = $current_step->format_settings['instance_settings'];

  // Translate button labels.
  foreach ($step_settings['buttons'] as $key => $label) {
    if (empty($label)) {
      continue;
    }
    $step_settings['buttons'][$key] = msnf_translate(array(
      $current_step->step_name,
      $current_step->bundle,
      "button_{$key}",
    ), $label);
  }

  // Try to load previous and next step.
  $previous_step = FALSE;
  $next_step = FALSE;
  $step_names = array_keys($steps);
  $current_index = array_search($current_step->step_name, $step_names);
  if ($current_index > 0 && isset($step_names[$current_index - 1])) {

    // Seems we have a step before the current one.
    $previous_step = $steps[$step_names[$current_index - 1]];
  }
  if ($current_index < count($steps) - 1 && isset($step_names[$current_index + 1])) {

    // Seems we have a step after the current one.
    $next_step = $steps[$step_names[$current_index + 1]];
  }

  // Update button labels if needed.
  if ($next_step && $step_settings['buttons']['next'] !== FALSE && drupal_strlen($step_settings['buttons']['next']) == 0) {

    // Set label of step as button label.
    $step_settings['buttons']['next'] = msnf_translate(array(
      $next_step->step_name,
      $next_step->bundle,
      'label',
    ), $next_step->label);
  }
  if ($previous_step && $step_settings['buttons']['previous'] !== FALSE && drupal_strlen($step_settings['buttons']['previous']) == 0) {

    // Set label of step as button label.
    $step_settings['buttons']['previous'] = msnf_translate(array(
      $previous_step->step_name,
      $previous_step->bundle,
      'label',
    ), $previous_step->label);
  }

  // Todo: 'actions' is not defined in all entity forms (e.g. term).
  if (!isset($form['actions'])) {
    $form['actions'] = array(
      '#type' => 'actions',
    );
  }

  // Create a list of all buttons the form may have.
  if ($current_index > 0 && $step_settings['buttons']['previous'] !== FALSE) {

    // Add "previous" button.
    $form['actions']['previous'] = array(
      '#type' => 'submit',
      '#access' => TRUE,
      '#value' => $step_settings['buttons']['previous'],
      '#weight' => 4,
      '#name' => 'previous',
      '#submit' => array(),
      '#limit_validation_errors' => array(),
    );
  }

  // Get the list of steps that may be skipped (no required fields and admin
  // decided to make this step skippable.
  $skippable_steps = _msnf_steps_get_skippable($form, $form_state);

  // Get list of remaining steps.
  $remaining_steps = array_slice($step_names, $current_index + 1);
  if ($current_index < count($steps) - 1) {

    // Add "next" button.
    if ($step_settings['buttons']['next'] !== FALSE) {
      $form['actions']['next'] = array(
        '#type' => 'submit',
        '#access' => TRUE,
        '#value' => $step_settings['buttons']['next'],
        '#weight' => 7,
        '#name' => 'next',
      );
    }

    // Is skipping the next step allowed?
    $skip_next = isset($skippable_steps[$next_step->step_name]);
    $skip_next &= $next_step->format_settings['instance_settings']['skip_non_required'];

    // The last step may never be skipped.
    $skip_next &= $current_index < count($steps) - 2;
    if ($skip_next && $step_settings['buttons']['skip'] !== FALSE) {

      // Add button to skip the next step.
      $form['actions']['skip'] = array(
        '#type' => 'submit',
        '#access' => TRUE,
        '#value' => $step_settings['buttons']['skip'],
        '#weight' => 8,
        '#name' => 'skip',
      );

      // Todo. If all further steps may be skipped ...
    }
  }
  if (isset($form['actions']['submit']) && $current_index != count($steps) - 1) {

    // Hide save button until we display the last step or all further steps may
    // be skipped.
    $form['actions']['submit']['#access'] = count(array_diff($remaining_steps, array_keys($skippable_steps))) == 0;
  }
}