You are here

protected function MediaCombinedUploadForm::processInputValues in Media Directories 8

Same name and namespace in other branches
  1. 2.x modules/media_directories_ui/src/Form/MediaCombinedUploadForm.php \Drupal\media_directories_ui\Form\MediaCombinedUploadForm::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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides AddMediaFormBase::processInputValues

File

modules/media_directories_ui/src/Form/MediaCombinedUploadForm.php, line 128

Class

MediaCombinedUploadForm
A form to upload media of different bundles.

Namespace

Drupal\media_directories_ui\Form

Code

protected function processInputValues(array $source_field_values, array $form, FormStateInterface $form_state) {
  $media = [];
  foreach ($source_field_values as $source_field_value) {
    $media_type = $this->mediaDirectoriesUiHelper
      ->getMediaType($source_field_value);
    $media_storage = $this->entityTypeManager
      ->getStorage('media');
    $source_field_name = $this
      ->getSourceFieldName($media_type);
    $field_config = $this->entityTypeManager
      ->getStorage('field_config')
      ->load('media.' . $media_type
      ->id() . '.' . $source_field_name);
    $destination = $this
      ->getUploadLocation($field_config
      ->getSettings());
    if ($this->fileSystem
      ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) {
      $source_field_value = file_move($source_field_value, $destination);
    }
    $media[] = $this
      ->createMediaFromValue($media_type, $media_storage, $source_field_name, $source_field_value, $form_state);
  }

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