You are here

public function BulkUploadForm::submitForm in Lightning Media 8

Same name and namespace in other branches
  1. 8.4 modules/lightning_media_bulk_upload/src/Form/BulkUploadForm.php \Drupal\lightning_media_bulk_upload\Form\BulkUploadForm::submitForm()
  2. 8.2 modules/lightning_media_bulk_upload/src/Form/BulkUploadForm.php \Drupal\lightning_media_bulk_upload\Form\BulkUploadForm::submitForm()
  3. 8.3 modules/lightning_media_bulk_upload/src/Form/BulkUploadForm.php \Drupal\lightning_media_bulk_upload\Form\BulkUploadForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

File

modules/lightning_media_bulk_upload/src/Form/BulkUploadForm.php, line 114

Class

BulkUploadForm
A form for uploading multiple media assets at once.

Namespace

Drupal\lightning_media_bulk_upload\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $bulk_create = [];
  $uploads = $form_state
    ->getValue([
    'dropzone',
    'uploaded_files',
  ]);
  foreach ($uploads as $upload) {

    // Create a file entity for the temporary file.

    /** @var \Drupal\file\FileInterface $file */
    $file = $this->entityTypeManager
      ->getStorage('file')
      ->create([
      'uri' => $upload['path'],
    ]);
    $file
      ->setTemporary();
    $file
      ->save();
    try {
      $entity = $this->helper
        ->createFromInput($file);
    } catch (IndeterminateBundleException $e) {
      drupal_set_message('error', (string) $e);
      continue;
    }
    $file = MediaHelper::useFile($entity, $file);
    $file
      ->setPermanent();
    $file
      ->save();
    $entity
      ->save();
    array_push($bulk_create, $bulk_create ? $entity
      ->id() : $entity);
  }
  if ($bulk_create) {

    /** @var \Drupal\media\MediaInterface $entity */
    $redirect = array_shift($bulk_create)
      ->toUrl('edit-form', [
      'query' => [
        'bulk_create' => $bulk_create,
      ],
    ]);
    $form_state
      ->setRedirectUrl($redirect);
  }
}