You are here

protected function AddFormBase::processInputValues in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media_library/src/Form/AddFormBase.php \Drupal\media_library\Form\AddFormBase::processInputValues()

Creates media items from source field input values.

Parameters

mixed[] $source_field_values: The values for source fields of the media items.

array $form: The complete form.

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

2 calls to AddFormBase::processInputValues()
FileUploadForm::uploadButtonSubmit in core/modules/media_library/src/Form/FileUploadForm.php
Submit handler for the upload button, inside the managed_file element.
OEmbedForm::addButtonSubmit in core/modules/media_library/src/Form/OEmbedForm.php
Submit handler for the add button.

File

core/modules/media_library/src/Form/AddFormBase.php, line 500

Class

AddFormBase
Provides a base class for creating media items from within the media library.

Namespace

Drupal\media_library\Form

Code

protected function processInputValues(array $source_field_values, array $form, FormStateInterface $form_state) {
  $media_type = $this
    ->getMediaType($form_state);
  $media_storage = $this->entityTypeManager
    ->getStorage('media');
  $source_field_name = $this
    ->getSourceFieldName($media_type);
  $media = array_map(function ($source_field_value) use ($media_type, $media_storage, $source_field_name) {
    return $this
      ->createMediaFromValue($media_type, $media_storage, $source_field_name, $source_field_value);
  }, $source_field_values);

  // Re-key the media items before setting them in the form state.
  $form_state
    ->set('media', array_values($media));

  // Save the selected items in the form state so they are remembered when an
  // item is removed.
  $media = $this->entityTypeManager
    ->getStorage('media')
    ->loadMultiple(explode(',', $form_state
    ->getValue('current_selection')));

  // Any ID can be passed to the form, so we have to check access.
  $form_state
    ->set('current_selection', array_filter($media, function ($media_item) {
    return $media_item
      ->access('view');
  }));
  $form_state
    ->setRebuild();
}