In the following bit of code i am using hook_form_alter to add some more form values to a system_settings_form.
If i add a basic text field, then the variable gets saved, however placing the following items in an array, nothing is getting saved.
I have tried using #tree as specified on the parent container containing the array items.
Sorry for the big code dump, but my math teacher always told me to show my work!
function mymodule_booking_coupons_form_alter(&$form, &$form_state, $form_id, $no_js_use = FALSE) {
if ($form_id == 'mymodule_booking_ui_admin') {
$form['coupons'] = array(
'#type' => 'fieldset',
'#title' => t('Coupons'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 1,
);
$form['coupons']['coupon_container'] = [
'#type' => 'container',
'#weight' => 80,
'#tree' => TRUE,
// Set up the wrapper so that AJAX will be able to replace the fieldset.
'#prefix' => '<div id="js-ajax-elements-wrapper">',
'#suffix' => '</div>',
];
$deltas = variable_get('coupon_deltas' , 0);
for ($delta = 0; $delta <= $deltas; $delta++) {
$form['coupons']['coupon_container'][$delta] = [
'#type' => 'container',
'#attributes' => [
'class' => ['container-inline'],
],
'#tree' => TRUE,
];
$form['coupons']['coupon_container'][$delta]['coupon_' . $delta . '_code'] = [
'#type' => 'textfield',
'#title' => t('Coupon Code'),
'#size' => 10,
'#default_value' => variable_get('coupon_' . $delta . '_code', ''),
];
$form['coupons']['coupon_container'][$delta]['coupon_' . $delta . '_discount'] = [
'#type' => 'textfield',
'#title' => t('Discount'),
'#size' => 10,
'#default_value' => variable_get('coupon_' . $delta . '_discount', ''),
];
$form['coupons']['coupon_container'][$delta]['coupon_' . $delta . '_type'] = [
'#type' => 'select',
'#title' => t('Type'),
'#options' => array(
0 => t('Fixed'),
1 => t('Percentage'),
),
'#default_value' => variable_get('coupon_' . $delta . '_type', 0),
];
$form['coupons']['coupon_container'][$delta]['remove_coupon'] = [
'#type' => 'submit',
'#value' => t('x'),
'#submit' => ['mymodule_booking_coupons_add_more_remove'],
'#ajax' => [
'callback' => 'mymodule_booking_coupons_add_more_remove_callback',
'wrapper' => 'js-ajax-elements-wrapper',
],
'#weight' => 50,
'#attributes' => [
'class' => ['button-small'],
],
'#name' => 'remove_coupon_' . $delta,
];
}
$form['coupons']['coupon_container']['add_name'] = [
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => ['mymodule_booking_coupons_add_more_add_one'],
'#ajax' => [
'callback' => 'mymodule_booking_coupons_add_more_add_one_callback',
'wrapper' => 'js-ajax-elements-wrapper',
],
'#weight' => 100,
];
}
}
function mymodule_booking_coupons_add_more_remove($form, &$form_state) {
$delta_remove = $form_state['triggering_element']['#parents'][1];
$k = array_search($delta_remove, $form['coupons']['coupon_deltas']);
unset($form['coupons']['coupon_deltas'][$k]);
$deltas = variable_get('coupon_deltas' , 1);
$deltas = $deltas - 1;
variable_set('coupon_deltas', $deltas);
$form_state['rebuild'] = TRUE;
drupal_get_messages();
}
function mymodule_booking_coupons_add_more_remove_callback($form, &$form_state) {
return $form['coupons']['coupon_container'];
}
function mymodule_booking_coupons_add_more_add_one($form, &$form_state) {
$deltas = variable_get('coupon_deltas' , 1);
$deltas = $deltas + 1;
variable_set('coupon_deltas', $deltas);
$form_state['rebuild'] = TRUE;
drupal_get_messages();
}
function mymodule_booking_coupons_add_more_add_one_callback($form, $form_state) {
return $form['coupons']['coupon_container'];
}
Update: I have placed a dpm($form_state) in the submit handler, and it shows the following array of submitted values - so they make it to this stage, they just dont get saved.