Trying to implement an unlimited value date field in my theme settings, using theme_form_system_theme_settings_alter to add the form inputs, including a button with an ajax callback function, which is also defined in my theme-settings.php file.

I've been trying to follow the examples in a blog post on multiple item values in forms and Drupal documentation on ajax forms.

I've tried adding the equivalent of the custom_registration_form function code from the blog post above to my theme_form_system_theme_settings_alter function, but can't get additional date fields to show up when my ajax-enabled button is clicked. I'm not sure if the theme_form_system_theme_settings_alter function is accessing and updating the form in the same way a regular form creation function would.

Any ideas on implementing an unlimited value field in theme settings would be appreciated, but I'm also just trying to wrap my head around the differences between theme_form_system_theme_settings_alter and a regular form creation function.

Update: I've got the "add" functionality in place in a custom module (which should work for theme settings as well), but struggling with the "remove" option for the individual entries. I've created a new post focusing on that piece of the functionality. The code I'm using (with some troubleshooting and comments) is included below:

function daily_hours_form($form, &$form_state) {

  // Fieldset container where we can add unlimited values
  $form['closed_mon_exceptions'] = 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_monday'] = isset($form_state['storage']['add_monday']) ? $form_state['storage']['add_monday'] : 0;
  if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#value'] == 'Add Open Monday') {
    $form_state['storage']['add_monday']++;
  }

  // If the user clicked to remove a day, need to regenerate the dates with that value removed
  if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#value'] == 'Remove Monday') {
    // get current values from form_state
    $closed_mon_dates_array = $form_state['values']['closed_mon_exceptions'];
    // remove the Add Open Monday button from this array
    array_pop($closed_mon_dates_array);
    // retrieve the index of the one we need to remove
    $remove_mon = $form_state['clicked_button']['#name'];
    // Try to remove that value from any array it's being used in
    //unset($form['closed_mon_exceptions'][$remove_mon]);
    unset($form_state['values']['closed_mon_exceptions'][$remove_mon]);
    unset($form_state['complete form']['closed_mon_exceptions'][$remove_mon]);
    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);
  } else {
    // if we're not trying to remove a monday, just get the saved values
    $closed_mon_dates_array = variable_get('closed_mon_exceptions');
  }
  dpm($closed_mon_dates_array);
  dpm($form_state);
  $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];
      // Remove the value for the button, if this data was pulled from
      // $form_state above
      if (count($current_date_val) == 4) {
        array_pop($current_date_val);
      }
    }
    dpm($current_date_val); // Just making sure it's formatted as expected...
    $form['closed_mon_exceptions'][$i] = array(
      '#type' => 'date',
      '#default_value' => $current_date_val,
      '#title' => t('Closed Monday Exception'),
      '#tree' => TRUE,
      '#prefix' => '<div id="monday-' . $i . '">',
      '#suffix' => '</div>',
    );
    // Add a remove button for each one
    $form['closed_mon_exceptions'][$i]['remove_day'] = array(
      '#type' => 'button',
      '#value' => t('Remove Monday'),
      '#name' => "$i",
      '#ajax' => array(
        'callback' => 'monday_remove',
        'wrapper' => 'closed_mon' // Update the whole fieldset
        //'wrapper' => 'monday-' . $i, // Alternately, try to update just this value
      ),
    );
  }

  // Check if user clicked "Add Open Monday"
  if ($form_state['storage']['add_monday'] > 0) {
    // Starting index for adding open Mondays
    $i = count($closed_mon_dates_array);
    $number_to_add = $form_state['storage']['add_monday'];
    $end_index = $i + $number_to_add;
    for ($index = $i; $index < $end_index; $index++) {
      $form['closed_mon_exceptions'][$index] = array(
        '#type' => 'date',
        '#default_value' => array(),
        '#title' => t('Closed Monday Exception'),
        '#tree' => TRUE,
        '#prefix' => '<div id="monday-' . $index . '">',
        '#suffix' => '</div>',
      );
      $form['closed_mon_exceptions'][$index]['remove_day'] = array(
        '#type' => 'button',
        '#value' => t('Remove Monday'),
        '#name' => "$index",
        '#ajax' => array(
          'callback' => 'monday_remove',
          'wrapper' => 'closed_mon',
          //'wrapper' => 'monday-' . $index,
        ),
      );
    }
  }
  $form['closed_mon_exceptions']['add_open_monday'] = array(
    '#type' => 'button',
    '#value' => t('Add Open Monday'),
    '#ajax' => array(
      'callback' => 'multi_date_ajax_callback',
      'wrapper' => 'closed_mon'
    ),
  );

  return system_settings_form($form);
}

function monday_remove($form, &$form_state) {
  //return ''; // Trying to update just the date + remove button field
  $form_state['rebuild'] = TRUE; // Tried adding this for troubleshooting; not seeing any difference
  return $form['closed_mon_exceptions'];
}

function multi_date_ajax_callback($form, &$form_state) {
  return $form['closed_mon_exceptions'];
}
  • Could you post actual code you are using? – Mołot Sep 30 '15 at 20:02
  • @Mołot I've added code – nkanderson Oct 7 '15 at 22:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.