View source
<?php
namespace Drupal\file_test\Form;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileTestSaveUploadFromForm extends FormBase {
protected $state;
protected $messenger;
public function __construct(StateInterface $state, MessengerInterface $messenger) {
$this->state = $state;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('state'), $container
->get('messenger'));
}
public function getFormId() {
return '_file_test_save_upload_from_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['file_test_upload'] = [
'#type' => 'file',
'#multiple' => TRUE,
'#title' => $this
->t('Upload a file'),
];
$form['file_test_replace'] = [
'#type' => 'select',
'#title' => $this
->t('Replace existing image'),
'#options' => [
FileSystemInterface::EXISTS_RENAME => $this
->t('Appends number until name is unique'),
FileSystemInterface::EXISTS_REPLACE => $this
->t('Replace the existing file'),
FileSystemInterface::EXISTS_ERROR => $this
->t('Fail with an error'),
],
'#default_value' => FileSystemInterface::EXISTS_RENAME,
];
$form['file_subdir'] = [
'#type' => 'textfield',
'#title' => $this
->t('Subdirectory for test file'),
'#default_value' => '',
];
$form['extensions'] = [
'#type' => 'textfield',
'#title' => $this
->t('Allowed extensions.'),
'#default_value' => '',
];
$form['allow_all_extensions'] = [
'#title' => t('Allow all extensions?'),
'#type' => 'radios',
'#options' => [
'false' => 'No',
'empty_array' => 'Empty array',
'empty_string' => 'Empty string',
],
'#default_value' => 'false',
];
$form['is_image_file'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Is this an image file?'),
'#default_value' => TRUE,
];
$form['error_message'] = [
'#type' => 'textfield',
'#title' => $this
->t('Custom error message.'),
'#default_value' => '',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->isValueEmpty('file_subdir')) {
$destination = 'temporary://' . $form_state
->getValue('file_subdir');
\Drupal::service('file_system')
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY);
}
else {
$destination = FALSE;
}
if ($form_state
->getValue('error_message')) {
$this->messenger
->addError($form_state
->getValue('error_message'));
}
$validators = [];
if ($form_state
->getValue('is_image_file')) {
$validators['file_validate_is_image'] = [];
}
$allow = $form_state
->getValue('allow_all_extensions');
if ($allow === 'empty_array') {
$validators['file_validate_extensions'] = [];
}
elseif ($allow === 'empty_string') {
$validators['file_validate_extensions'] = [
'',
];
}
elseif (!$form_state
->isValueEmpty('extensions')) {
$validators['file_validate_extensions'] = [
$form_state
->getValue('extensions'),
];
}
if ($this->state
->get('file_test.disable_error_collection')) {
define('SIMPLETEST_COLLECT_ERRORS', FALSE);
}
$form['file_test_upload']['#upload_validators'] = $validators;
$form['file_test_upload']['#upload_location'] = $destination;
$this->messenger
->addStatus($this
->t('Number of error messages before _file_save_upload_from_form(): @count.', [
'@count' => count($this->messenger
->messagesByType(MessengerInterface::TYPE_ERROR)),
]));
$file = _file_save_upload_from_form($form['file_test_upload'], $form_state, 0, $form_state
->getValue('file_test_replace'));
$this->messenger
->addStatus($this
->t('Number of error messages after _file_save_upload_from_form(): @count.', [
'@count' => count($this->messenger
->messagesByType(MessengerInterface::TYPE_ERROR)),
]));
if ($file) {
$form_state
->setValue('file_test_upload', $file);
$this->messenger
->addStatus($this
->t('File @filepath was uploaded.', [
'@filepath' => $file
->getFileUri(),
]));
$this->messenger
->addStatus($this
->t('File name is @filename.', [
'@filename' => $file
->getFilename(),
]));
$this->messenger
->addStatus($this
->t('File MIME type is @mimetype.', [
'@mimetype' => $file
->getMimeType(),
]));
$this->messenger
->addStatus($this
->t('You WIN!'));
}
elseif ($file === FALSE) {
$this->messenger
->addError($this
->t('Epic upload FAIL!'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}