protected function LibraryItemFieldWidget::formMultipleElements in Library 8
Special handling to create form elements for multiple values.
Handles generic features for multiple fields:
- number of widgets
- AHAH-'add more' button
- table display and drag-n-drop value reordering
Overrides WidgetBase::formMultipleElements
File
- src/
Plugin/ Field/ FieldWidget/ LibraryItemFieldWidget.php, line 282
Class
- LibraryItemFieldWidget
- Plugin implementation of the 'library_item_field_widget' widget.
Namespace
Drupal\library\Plugin\Field\FieldWidgetCode
protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) : array {
// Taken from inline_entity_form.
$element = parent::formMultipleElements($items, $form, $form_state);
// If we're using ulimited cardinality we don't display one empty item. Form
// validation will kick in if left empty which esentially means people won't
// be able to submit w/o creating another entity.
if (!$form_state
->isSubmitted() && $element['#cardinality'] == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && $element['#max_delta'] > 0) {
$max = $element['#max_delta'];
unset($element[$max]);
$element['#max_delta'] = $max - 1;
$items
->removeItem($max);
// Decrement the items count.
$field_name = $element['#field_name'];
$parents = $element[0]['#field_parents'];
$field_state = static::getWidgetState($parents, $field_name, $form_state);
$field_state['items_count']--;
static::setWidgetState($parents, $field_name, $form_state, $field_state);
}
return $element;
}