You are here

protected static function MediaLibraryWidget::getNewMediaItems in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getNewMediaItems()
  2. 10 core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php \Drupal\media_library\Plugin\Field\FieldWidget\MediaLibraryWidget::getNewMediaItems()

Gets newly selected media items.

Parameters

array $element: The wrapping element for this widget.

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

Return value

\Drupal\media\MediaInterface[] An array of selected media items.

3 calls to MediaLibraryWidget::getNewMediaItems()
MediaLibraryWidget::addItems in core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
Updates the field state and flags the form for rebuild.
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 882

Class

MediaLibraryWidget
Plugin implementation of the 'media_library_widget' widget.

Namespace

Drupal\media_library\Plugin\Field\FieldWidget

Code

protected static function getNewMediaItems(array $element, FormStateInterface $form_state) {

  // Get the new media IDs passed to our hidden button. 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 = $form_state
    ->getUserInput();
  $path = $element['#parents'];
  $value = NestedArray::getValue($values, $path);
  if (!empty($value['media_library_selection'])) {
    $ids = explode(',', $value['media_library_selection']);
    $ids = array_filter($ids, 'is_numeric');
    if (!empty($ids)) {

      /** @var \Drupal\media\MediaInterface[] $media */
      return Media::loadMultiple($ids);
    }
  }
  return [];
}