You are here

protected function DropzoneJsEbWidget::getFiles in DropzoneJS 8

Same name and namespace in other branches
  1. 8.2 modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php \Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget\DropzoneJsEbWidget::getFiles()

Gets uploaded files.

We implement this to allow child classes to operate on different entity type while still having access to the files in the validate callback here.

Parameters

array $form: Form structure.

FormStateInterface $form_state: Form state object.

Return value

\Drupal\file\FileInterface[] Array of uploaded files.

2 calls to DropzoneJsEbWidget::getFiles()
DropzoneJsEbWidget::prepareEntities in modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php
DropzoneJsEbWidget::validate in modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php

File

modules/eb_widget/src/Plugin/EntityBrowser/Widget/DropzoneJsEbWidget.php, line 196

Class

DropzoneJsEbWidget
Provides an Entity Browser widget that uploads new files.

Namespace

Drupal\dropzonejs_eb_widget\Plugin\EntityBrowser\Widget

Code

protected function getFiles(array $form, FormStateInterface $form_state) {
  $config = $this
    ->getConfiguration();
  $additional_validators = [
    'file_validate_size' => [
      Bytes::toInt($config['settings']['max_filesize']),
      0,
    ],
  ];
  $files = $form_state
    ->get([
    'dropzonejs',
    $this
      ->uuid(),
    'files',
  ]);
  if (!$files) {
    $files = [];
  }

  // We do some casting because $form_state->getValue() might return NULL.
  foreach ((array) $form_state
    ->getValue([
    'upload',
    'uploaded_files',
  ], []) as $file) {
    if (file_exists($file['path'])) {
      $entity = $this->dropzoneJsUploadSave
        ->createFile($file['path'], $this
        ->getUploadLocation(), $config['settings']['extensions'], $this->currentUser, $additional_validators);
      if ($entity) {
        $files[] = $entity;
      }
    }
  }
  if ($form['widget']['upload']['#max_files']) {
    $files = array_slice($files, -$form['widget']['upload']['#max_files']);
  }
  $form_state
    ->set([
    'dropzonejs',
    $this
      ->uuid(),
    'files',
  ], $files);
  return $files;
}