You are here

public function MediaBulkUploadForm::submitForm in Media Bulk Upload 8

Submit handler to create the file entities and media entities.

Parameters

array $form: The form render array.

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

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

Overrides FormInterface::submitForm

File

src/Form/MediaBulkUploadForm.php, line 303

Class

MediaBulkUploadForm
Class BulkMediaUploadForm.

Namespace

Drupal\media_bulk_upload\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  $mediaBundleConfigId = $values['media_bundle_config'];

  /** @var MediaBulkConfigInterface $mediaBulkConfig */
  $mediaBulkConfig = $this->mediaBulkConfigStorage
    ->load($mediaBundleConfigId);
  $files = $values['dropzonejs']['uploaded_files'];
  $mediaTypes = $this->mediaSubFormManager
    ->getMediaTypeManager()
    ->getBulkMediaTypes($mediaBulkConfig);
  $mediaType = reset($mediaTypes);
  $mediaFormDisplay = $this->mediaSubFormManager
    ->getMediaFormDisplay($mediaBulkConfig, $mediaType);
  $this
    ->prepareFormValues($form_state);
  $savedMediaItems = [];
  foreach ($files as $file) {
    try {
      $media = $this
        ->processFile($mediaBulkConfig, $file);
      if (!$media) {
        continue;
      }
      if ($this->mediaSubFormManager
        ->validateMediaFormDisplayUse($mediaBulkConfig)) {
        $extracted = $mediaFormDisplay
          ->extractFormValues($media, $form['fields']['shared'], $form_state);
        $this
          ->copyFormValuesToEntity($media, $extracted, $form_state);
      }
      $media
        ->save();
      $savedMediaItems[] = $media;
    } catch (\Exception $e) {
      watchdog_exception('media_bulk_upload', $e);
    }
  }
  if (!empty($savedMediaItems)) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('@count media item(s) are created.', [
      '@count' => count($savedMediaItems),
    ]));
  }
}