-
-
Save edutrul/416d5feeee9fa08787e8fffb65433179 to your computer and use it in GitHub Desktop.
Drupal 'add more' and 'remove single' AJAX buttons on multi value custom field using FormAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input.form-submit.button-small { | |
padding: 4px 8px; | |
font-weight: bold; | |
} | |
.container-inline input.form-submit.button-small + .ajax-progress.ajax-progress-throbber .throbber { | |
position: absolute; | |
left: 19px; | |
margin-top: 7px; | |
} | |
.container-inline input.form-submit.button-small + .ajax-progress.ajax-progress-throbber .message { | |
display: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mymodule_ajax_example_add_more($form, &$form_state) { | |
$form['field_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>', | |
]; | |
$form_state['field_deltas'] = isset($form_state['field_deltas']) ? $form_state['field_deltas'] : range(0, 3); | |
$field_count = $form_state['field_deltas']; | |
foreach ($field_count as $delta) { | |
$form['field_container'][$delta] = [ | |
'#type' => 'container', | |
'#attributes' => [ | |
'class' => ['container-inline'], | |
], | |
'#tree' => TRUE, | |
]; | |
$form['field_container'][$delta]['field1'] = [ | |
'#type' => 'textfield', | |
'#title' => t('Field 1 - ' . $delta), | |
'#size' => 10, | |
]; | |
$form['field_container'][$delta]['field2'] = [ | |
'#type' => 'textfield', | |
'#title' => t('Field 2 - ' . $delta), | |
'#size' => 10, | |
]; | |
$form['field_container'][$delta]['remove_name'] = [ | |
'#type' => 'submit', | |
'#value' => t('-'), | |
'#submit' => ['mymodule_ajax_example_add_more_remove'], | |
// See the examples in ajax_example.module for more details on the | |
// properties of #ajax. | |
'#ajax' => [ | |
'callback' => 'mymodule_ajax_example_add_more_remove_callback', | |
'wrapper' => 'js-ajax-elements-wrapper', | |
], | |
'#weight' => -50, | |
'#attributes' => [ | |
'class' => ['button-small'], | |
], | |
'#name' => 'remove_name_' . $delta, | |
]; | |
} | |
$form['field_container']['add_name'] = [ | |
'#type' => 'submit', | |
'#value' => t('Add one more'), | |
'#submit' => ['mymodule_ajax_example_add_more_add_one'], | |
// See the examples in ajax_example.module for more details on the | |
// properties of #ajax. | |
'#ajax' => [ | |
'callback' => 'mymodule_ajax_example_add_more_add_one_callback', | |
'wrapper' => 'js-ajax-elements-wrapper', | |
], | |
'#weight' => 100, | |
]; | |
$form['other_field'] = [ | |
'#type' => 'textfield', | |
'#title' => t('Other field'), | |
]; | |
return $form; | |
} | |
function mymodule_ajax_example_add_more_remove($form, &$form_state) { | |
$delta_remove = $form_state['triggering_element']['#parents'][1]; | |
$k = array_search($delta_remove, $form_state['field_deltas']); | |
unset($form_state['field_deltas'][$k]); | |
$form_state['rebuild'] = TRUE; | |
drupal_get_messages(); | |
} | |
function mymodule_ajax_example_add_more_remove_callback($form, &$form_state) { | |
return $form['field_container']; | |
} | |
function mymodule_ajax_example_add_more_add_one($form, &$form_state) { | |
$form_state['field_deltas'][] = count($form_state['field_deltas']) > 0 ? max($form_state['field_deltas']) + 1 : 0; | |
$form_state['rebuild'] = TRUE; | |
drupal_get_messages(); | |
} | |
function mymodule_ajax_example_add_more_add_one_callback($form, $form_state) { | |
return $form['field_container']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment