Upload.php in Lightning Media 8.3
File
src/Element/Upload.php
View source
<?php
namespace Drupal\lightning_media\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\File as FileElement;
use Drupal\file\Entity\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Upload extends FileElement {
public function getInfo() {
$info = parent::getInfo();
$info['#required'] = FALSE;
$info['#upload_location'] = 'public://';
$info['#upload_validators'] = [];
$info['#element_validate'] = [
[
static::class,
'validate',
],
];
return $info;
}
public static function validate(array &$element, FormStateInterface $form_state) {
if ($element['#value']) {
$file = File::load($element['#value']);
$errors = file_validate($file, $element['#upload_validators']);
if ($errors) {
foreach ($errors as $error) {
$form_state
->setError($element, $error);
}
static::delete($element);
}
}
elseif ($element['#required']) {
$form_state
->setError($element, t('You must upload a file.'));
}
}
public static function delete(array $element) {
if ($element['#value']) {
$file = File::load($element['#value']);
$file
->delete();
$uri = $file
->getFileUri();
if (file_exists($uri)) {
\Drupal::service('file_system')
->unlink($uri);
}
}
}
public static function processFile(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#name'] = implode('_', $element['#parents']);
$form_state
->setHasFileElement();
return parent::processFile($element, $form_state, $complete_form);
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$file_system = \Drupal::service('file_system');
$id = implode('_', $element['#parents']);
$upload = \Drupal::request()->files
->get($id);
if ($upload instanceof UploadedFile) {
$destination = $file_system
->realPath($element['#upload_location']);
$name = file_munge_filename($upload
->getClientOriginalName(), NULL);
$name = version_compare(\Drupal::VERSION, '8.7.0', '>=') ? $file_system
->createFilename($name, $destination) : call_user_func('file_create_filename', $name, $destination);
$name = $upload
->move($destination, $name)
->getFilename();
$uri = $element['#upload_location'];
if (substr($uri, -1) != '/') {
$uri .= '/';
}
$uri .= $name;
$file = File::create([
'uri' => $uri,
'uid' => \Drupal::currentUser()
->id(),
]);
$file
->setTemporary();
$file
->save();
\Drupal::request()->files
->remove($id);
return $file
->id();
}
else {
return NULL;
}
}
}
Classes
Name |
Description |
Upload |
A form element to handle file uploads. |