function MediaLibraryWidget::getFieldState

Same name in other branches
  1. 9 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getFieldState()
  2. 10 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getFieldState()
  3. 11.x core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getFieldState()

Gets the field state for the widget.

Parameters

array $element: The wrapping element for this widget.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array[] An array of arrays with the following key/value pairs:

  • items: (array) An array of selections.

    • target_id: (int) A media entity ID.
    • weight: (int) A weight for the selection.
4 calls to MediaLibraryWidget::getFieldState()
MediaLibraryWidget::addItems in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Updates the field state and flags the form for rebuild.
MediaLibraryWidget::removeItem in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Submit callback for remove buttons.
MediaLibraryWidget::updateWidget in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
AJAX callback to update the widget when the selection changes.
MediaLibraryWidget::validateItems in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Validates that newly selected items can be added to the widget.

File

core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php, line 916

Class

MediaLibraryWidget
Plugin implementation of the 'media_library_widget' widget.

Namespace

Drupal\media_library\Plugin\Field\FieldWidget

Code

protected static function getFieldState(array $element, FormStateInterface $form_state) {
    // Default to using the current selection if the form is new.
    $path = $element['#parents'];
    // We need to use the actual user input, since when #limit_validation_errors
    // is used, the unvalidated user input is not added to the form state.
    // @see FormValidator::handleErrorsWithLimitedValidation()
    $values = NestedArray::getValue($form_state->getUserInput(), $path);
    $selection = isset($values['selection']) ? $values['selection'] : [];
    $widget_state = static::getWidgetState($element['#field_parents'], $element['#field_name'], $form_state);
    $widget_state['items'] = isset($widget_state['items']) ? $widget_state['items'] : $selection;
    return $widget_state;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.