Commit 984ea2a6 authored by alexpott's avatar alexpott

Issue #2201709 by yched: Move support for "add more" button into WidgetBase.

parent e3784599
......@@ -218,11 +218,11 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
'#value' => t('Add another item'),
'#attributes' => array('class' => array('field-add-more-submit')),
'#limit_validation_errors' => array(array_merge($parents, array($field_name))),
'#submit' => array('field_add_more_submit'),
'#submit' => array(array(get_class($this), 'addMoreSubmit')),
'#ajax' => array(
'callback' => 'field_add_more_js',
'wrapper' => $wrapper_id,
'effect' => 'fade',
'callback' => array(get_class($this), 'addMoreAjax'),
'wrapper' => $wrapper_id,
'effect' => 'fade',
),
);
}
......@@ -231,6 +231,50 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f
return $elements;
}
/**
* Submission handler for the "Add another item" button.
*/
public static function addMoreSubmit(array $form, array &$form_state) {
$button = $form_state['triggering_element'];
// Go one level up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
// Increment the items count.
$field_state = field_form_get_state($parents, $field_name, $form_state);
$field_state['items_count']++;
field_form_set_state($parents, $field_name, $form_state, $field_state);
$form_state['rebuild'] = TRUE;
}
/**
* Ajax callback for the "Add another item" button.
*
* This returns the new page content to replace the page content made obsolete
* by the form submission.
*/
public static function addMoreAjax(array $form, array $form_state) {
$button = $form_state['triggering_element'];
// Go one level up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
// Ensure the widget allows adding additional items.
if ($element['#cardinality'] != FieldDefinitionInterface::CARDINALITY_UNLIMITED) {
return;
}
// Add a DIV around the delta receiving the Ajax effect.
$delta = $element['#max_delta'];
$element[$delta]['#prefix'] = '<div class="ajax-new-content">' . (isset($element[$delta]['#prefix']) ? $element[$delta]['#prefix'] : '');
$element[$delta]['#suffix'] = (isset($element[$delta]['#suffix']) ? $element[$delta]['#suffix'] : '') . '</div>';
return $element;
}
/**
* Generates the form element for a single copy of the widget.
*/
......
......@@ -139,58 +139,6 @@ function field_form_element_after_build($element, &$form_state) {
return $element;
}
/**
* Form submission handler for the "Add another item" button of a field form.
*
* This handler is run regardless of whether JS is enabled or not. It makes
* changes to the form state. If the button was clicked with JS disabled, then
* the page is reloaded with the complete rebuilt form. If the button was
* clicked with JS enabled, then Drupal\system\FormAjaxController::content()
* calls field_add_more_js() to return just the changed part of the form.
*/
function field_add_more_submit($form, &$form_state) {
$button = $form_state['triggering_element'];
// Go one level up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
$field_name = $element['#field_name'];
$parents = $element['#field_parents'];
// Increment the items count.
$field_state = field_form_get_state($parents, $field_name, $form_state);
$field_state['items_count']++;
field_form_set_state($parents, $field_name, $form_state, $field_state);
$form_state['rebuild'] = TRUE;
}
/**
* Ajax callback: Responds to a new empty widget being added to the form.
*
* This returns the new page content to replace the page content made obsolete
* by the form submission.
*
* @see field_add_more_submit()
*/
function field_add_more_js($form, $form_state) {
$button = $form_state['triggering_element'];
// Go one level up in the form, to the widgets container.
$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
// Ensure the widget allows adding additional items.
if ($element['#cardinality'] != FieldDefinitionInterface::CARDINALITY_UNLIMITED) {
return;
}
// Add a DIV around the delta receiving the Ajax effect.
$delta = $element['#max_delta'];
$element[$delta]['#prefix'] = '<div class="ajax-new-content">' . (isset($element[$delta]['#prefix']) ? $element[$delta]['#prefix'] : '');
$element[$delta]['#suffix'] = (isset($element[$delta]['#suffix']) ? $element[$delta]['#suffix'] : '') . '</div>';
return $element;
}
/**
* Retrieves processing information about a field from $form_state.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment