View source
<?php
namespace Drupal\lightning_media_bulk_upload\Form;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\lightning_core\Element;
use Drupal\lightning_media\Exception\IndeterminateBundleException;
use Drupal\lightning_media\MediaHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BulkUploadForm extends FormBase {
protected $entityTypeManager;
protected $helper;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MediaHelper $helper, TranslationInterface $translator) {
$this->entityTypeManager = $entity_type_manager;
$this->helper = $helper;
$this
->setStringTranslation($translator);
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('lightning.media_helper'), $container
->get('string_translation'));
}
public function getFormId() {
return 'bulk_upload_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$extensions = $this->helper
->getFileExtensions(TRUE);
$form['dropzone'] = [
'#type' => 'dropzonejs',
'#dropzone_description' => $this
->t('Drag files here to upload them'),
'#extensions' => implode(' ', $extensions),
];
$form['continue'] = [
'#type' => 'submit',
'#value' => $this
->t('Continue'),
];
$max_size = version_compare(\Drupal::VERSION, '8.7.0', '>=') ? Environment::getUploadMaxSize() : call_user_func('file_upload_max_size');
$variables = [
'@max_size' => static::bytesToString($max_size),
'@extensions' => Element::oxford($extensions, $this
->t('and')),
];
$form['dropzone']['#description'] = $this
->t('You can upload as many files as you like. Each file can be up to @max_size in size. The following file extensions are accepted: @extensions', $variables);
return $form;
}
public static function bytesToString($bytes) {
$units = array_map('t', [
'bytes',
'KB',
'MB',
'GB',
'TB',
]);
while ($bytes > 1024) {
$bytes /= 1024;
array_shift($units);
}
return $bytes . ' ' . reset($units);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$bulk_create = [];
$uploads = $form_state
->getValue([
'dropzone',
'uploaded_files',
]);
foreach ($uploads as $upload) {
$file = $this->entityTypeManager
->getStorage('file')
->create([
'uri' => $upload['path'],
'uid' => $this
->currentUser()
->id(),
]);
$file
->setTemporary();
$file
->save();
try {
$entity = $this->helper
->createFromInput($file);
} catch (IndeterminateBundleException $e) {
$this
->messenger()
->addError((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) {
$redirect = array_shift($bulk_create)
->toUrl('edit-form', [
'query' => [
'bulk_create' => $bulk_create,
],
]);
$form_state
->setRedirectUrl($redirect);
}
}
}