You are here

protected static function MediaLibrary::getNewMediaItem in Media Library Form API Element 2.x

Same name and namespace in other branches
  1. 8 src/Element/MediaLibrary.php \Drupal\media_library_form_element\Element\MediaLibrary::getNewMediaItem()

Gets newly selected media item.

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 selected media item.

1 call to MediaLibrary::getNewMediaItem()
MediaLibrary::validateItem in src/Element/MediaLibrary.php
Validates that newly selected items can be added to the widget.

File

src/Element/MediaLibrary.php, line 437

Class

MediaLibrary
Provides a Media library form element.

Namespace

Drupal\media_library_form_element\Element

Code

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

  // Get the new media IDs passed to our hidden button.
  $values = $form_state
    ->getValues();
  $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 [];
}