diff --git a/core/lib/Drupal/Core/Field/WidgetBase.php b/core/lib/Drupal/Core/Field/WidgetBase.php index 5f7ce3c..5dbf58c 100644 --- a/core/lib/Drupal/Core/Field/WidgetBase.php +++ b/core/lib/Drupal/Core/Field/WidgetBase.php @@ -193,15 +193,38 @@ protected function formMultipleElements(FieldItemListInterface $items, array &$f if ($is_multiple) { // We name the element '_weight' to avoid clashing with elements // defined by widget. $element['_weight'] = array( '#type' => 'weight', '#title' => $this->t('Weight for row @number', array('@number' => $delta + 1)), '#title_display' => 'invisible', // Note: this 'delta' is the FAPI #type 'weight' element's property. '#delta' => $max, '#default_value' => $items[$delta]->_weight ?: $delta, '#weight' => 100, ); + + $remove_wrapper = array_merge($parents, array( + $field_name, + $delta, + 'remove', + 'item' + )); + $remove_wrapper_id = Html::getUniqueId(implode('-', $remove_wrapper)); + $element['#prefix'] = "
"; + $element['#suffix'] = '
'; + $element['remove_item'] = array( + '#type' => 'submit', + '#value' => $this->t('Remove item'), + '#submit' => array(array(get_class($this), 'removeItemSubmit')), + '#ajax' => array( + 'callback' => array($this, 'removeItemAjax'), + 'wrapper' => $remove_wrapper_id, + 'effect' => 'fade', + ), + '#name' => implode('_', $remove_wrapper), + '#attributes' => array('class' => array('field-remove-item-submit')), + '#limit_validation_errors' => array(array_merge($parents, array($field_name))), + ]; } $elements[$delta] = $element; @@ -263,6 +286,34 @@ public static function afterBuild(array $element, FormStateInterface $form_state return $element; } + public static function removeItemSubmit(array $form, FormStateInterface $form_state) { + $button = $form_state->getTriggeringElement(); + + // Go one level up in the form, to the widgets container. + $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1)); + $container_element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2)); + $field_name = $container_element['#field_name']; + $field_parents = $element['#field_parents']; + $delta = $element['#delta']; + $field_values = &$form_state->getValue($container_element['#parents']); + unset($field_values[$delta]); + + // Increment the items count. + $field_state = static::getWidgetState($field_parents, $field_name, $form_state); + $field_state['items_count']--; + static::setWidgetState($field_parents, $field_name, $form_state, $field_state); + + $form_state->setRebuild(); + } + + public static function removeItemAjax(array $form, FormStateInterface $form_state) { + return array( + '#type' => 'hidden', + '#attributes' => array( + 'data-description' => 'It is a requirement to return something to make an ajax call hide the entire element.' + ) + ); + } /** * Submission handler for the "Add another item" button. */