function FileTestForm::submitForm
Same name in other branches
- 9 core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm::submitForm()
- 10 core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm::submitForm()
- 11.x core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm::submitForm()
Overrides FormInterface::submitForm
File
-
core/
modules/ file/ tests/ file_test/ src/ Form/ FileTestForm.php, line 83
Class
- FileTestForm
- File test form class.
Namespace
Drupal\file_test\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
// Process the upload and perform validation. Note: we're using the
// form value for the $replace parameter.
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;
}
// Setup validators.
$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'),
];
}
// The test for \Drupal::service('file_system')->moveUploadedFile()
// triggering a warning is unavoidable. We're interested in what happens
// afterwards in file_save_upload().
if (\Drupal::state()->get('file_test.disable_error_collection')) {
define('SIMPLETEST_COLLECT_ERRORS', FALSE);
}
$file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state->getValue('file_test_replace'));
if ($file) {
$form_state->setValue('file_test_upload', $file);
\Drupal::messenger()->addStatus(t('File @filepath was uploaded.', [
'@filepath' => $file->getFileUri(),
]));
\Drupal::messenger()->addStatus(t('File name is @filename.', [
'@filename' => $file->getFilename(),
]));
\Drupal::messenger()->addStatus(t('File MIME type is @mimetype.', [
'@mimetype' => $file->getMimeType(),
]));
\Drupal::messenger()->addStatus(t('You WIN!'));
}
elseif ($file === FALSE) {
\Drupal::messenger()->addError(t('Epic upload FAIL!'));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.