brrocks28
9/16/2016 - 9:44 AM

ADD MORE WITH REMOVE //SAMPLE CODE

ADD MORE WITH REMOVE //SAMPLE CODE


function ssf_form($form, &$form_state) {
  // CLOSED MONDAY EXCEPTIONS
  // Fieldset container where we can add unlimited values
  $form['variable_data'] = array(
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#prefix' => '<div id="closed_mon">',
    '#suffix' => '</div>',
    '#description' => t('Exceptions to our typical closed Mondays.'),
  );

  // Add a flag to add on additional Mondays if the user clicks "Add Open Monday"
  $form_state['storage']['add_one'] = FALSE;
  if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == 'Add Open Monday') {
    $form_state['storage']['add_one'] = TRUE;
  }

  $form_state['storage']['monday_init'] = isset($form_state['storage']['monday_init']) ? $form_state['storage']['monday_init'] : TRUE;

  // First time through, grab the values from the DB
  if ($form_state['storage']['monday_init']) {
    $closed_mon_dates_array = variable_get('variable_data');
    $form_state['storage']['monday_init'] = FALSE;
    // On subsequent renderings, grab the values from the values array
  }
  else {
    // get current values from form_state
    $closed_mon_dates_array = $form_state['values']['variable_data'];
    // remove the Add Open Monday button from this array
    array_pop($closed_mon_dates_array);
    $num_closed_mondays = count($closed_mon_dates_array);
  }

  // Get all the stored closed Mondays exceptions and remove the user-selected index
  if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == 'Remove variable') {
    // retrieve the index of the one we need to remove
    // preg_match required to get around Drupal bug related to multiple
    // submit buttons with the same name
    preg_match('/\d+/', $form_state['triggering_element']['#name'], $remove_mon);
    $remove_mon = $remove_mon[0];
    unset($closed_mon_dates_array[$remove_mon]);
    // reorder the array, so we have expected 0 through (count-1) indices
    $closed_mon_dates_array = array_values($closed_mon_dates_array);

    // Update the form values that will be submitted
    unset($form_state['input']['variable_data'][$remove_mon]);
    $form_state['input']['variable_data'] = array_values($form_state['input']['variable_data']);
    // or re-rendered to the form
    unset($form_state['complete form']['variable_data'][$remove_mon]);
    unset($form_state['values']['variable_data'][$remove_mon]);
  }

  $num_closed_mondays = count($closed_mon_dates_array);

  // Add all the current closed Monday exceptions
  for ($i = 0; $i < $num_closed_mondays; $i++) {
    if (isset($closed_mon_dates_array[$i])) {
      $current_date_val = $closed_mon_dates_array[$i];
      if (count($current_date_val) == 4) {
        array_pop($current_date_val);
      }
    }
    
    $form['variable_data'][$i] = array(
      '#type' => 'date',
      '#default_value' => $current_date_val,
      '#title' => t('Closed Monday Exception'),
      '#prefix' => '<div id="monday-' . $i . '">',
      '#suffix' => '</div>',
    );
    // Add a remove button for each one
    $form['variable_data'][$i]['remove_day'] = array(
      '#type' => 'button',
      '#value' => t('Remove variable'),
      '#name' => "removemon-$i", // need to create unique names, regardless of tree structure
      '#ajax' => array(
        'callback' => 'monday_ajax_callback',
        'wrapper' => 'closed_mon'
      ),
    );
  }

  // Check if user clicked "Add Open Monday"
  if ($form_state['storage']['add_one']) {
    // index for this one is at the end of the stored values
    $index = count($closed_mon_dates_array);
    $form['variable_data'][$index] = array(
      '#type' => 'date',
      '#default_value' => array(),
      '#title' => t('Closed Monday Exception'),
      '#prefix' => '<div id="monday-' . $index . '">',
      '#suffix' => '</div>',
    );
    $form['variable_data'][$index]['remove_day'] = array(
      '#type' => 'button',
      '#value' => t('Remove variable'),
      '#name' => "removemon-$index", // need to create unique names, regardless of tree structure
      '#ajax' => array(
        'callback' => 'monday_ajax_callback',
        'wrapper' => 'closed_mon',
      ),
    );
  }

  $form['variable_data']['add_open_monday'] = array(
    '#type' => 'button',
    '#value' => t('Add Open Monday'),
    '#name' => t('add-monday'),
    '#ajax' => array(
      'callback' => 'monday_ajax_callback',
      'wrapper' => 'closed_mon'
    ),
  );

  return system_settings_form($form);
}

function monday_ajax_callback($form, &$form_state) {
  return $form['variable_data'];
}

function holiday_ajax_callback($form, &$form_state) {
  return $form['holidays'];
}