3 namespace Drupal\file_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Messenger\MessengerInterface;
8 use Drupal\Core\State\StateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * File test form class.
14 class FileTestSaveUploadFromForm extends FormBase {
17 * Stores the state storage service.
19 * @var \Drupal\Core\State\StateInterface
26 * @var \Drupal\Core\Messenger\MessengerInterface
31 * Constructs a FileTestSaveUploadFromForm object.
33 * @param \Drupal\Core\State\StateInterface $state
34 * The state key value store.
35 * @param \Drupal\Core\Messenger\MessengerInterface $messenger
38 public function __construct(StateInterface $state, MessengerInterface $messenger) {
39 $this->state = $state;
40 $this->messenger = $messenger;
46 public static function create(ContainerInterface $container) {
48 $container->get('state'),
49 $container->get('messenger')
56 public function getFormId() {
57 return '_file_test_save_upload_from_form';
63 public function buildForm(array $form, FormStateInterface $form_state) {
64 $form['file_test_upload'] = [
67 '#title' => $this->t('Upload a file'),
69 $form['file_test_replace'] = [
71 '#title' => $this->t('Replace existing image'),
73 FILE_EXISTS_RENAME => $this->t('Appends number until name is unique'),
74 FILE_EXISTS_REPLACE => $this->t('Replace the existing file'),
75 FILE_EXISTS_ERROR => $this->t('Fail with an error'),
77 '#default_value' => FILE_EXISTS_RENAME,
79 $form['file_subdir'] = [
80 '#type' => 'textfield',
81 '#title' => $this->t('Subdirectory for test file'),
82 '#default_value' => '',
85 $form['extensions'] = [
86 '#type' => 'textfield',
87 '#title' => $this->t('Allowed extensions.'),
88 '#default_value' => '',
91 $form['allow_all_extensions'] = [
92 '#type' => 'checkbox',
93 '#title' => $this->t('Allow all extensions?'),
94 '#default_value' => FALSE,
97 $form['is_image_file'] = [
98 '#type' => 'checkbox',
99 '#title' => $this->t('Is this an image file?'),
100 '#default_value' => TRUE,
103 $form['error_message'] = [
104 '#type' => 'textfield',
105 '#title' => $this->t('Custom error message.'),
106 '#default_value' => '',
111 '#value' => $this->t('Submit'),
119 public function validateForm(array &$form, FormStateInterface $form_state) {
120 // Process the upload and perform validation. Note: we're using the
121 // form value for the $replace parameter.
122 if (!$form_state->isValueEmpty('file_subdir')) {
123 $destination = 'temporary://' . $form_state->getValue('file_subdir');
124 file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
127 $destination = FALSE;
130 // Preset custom error message if requested.
131 if ($form_state->getValue('error_message')) {
132 $this->messenger->addError($form_state->getValue('error_message'));
137 if ($form_state->getValue('is_image_file')) {
138 $validators['file_validate_is_image'] = [];
141 if ($form_state->getValue('allow_all_extensions')) {
142 $validators['file_validate_extensions'] = [];
144 elseif (!$form_state->isValueEmpty('extensions')) {
145 $validators['file_validate_extensions'] = [$form_state->getValue('extensions')];
148 // The test for drupal_move_uploaded_file() triggering a warning is
149 // unavoidable. We're interested in what happens afterwards in
150 // _file_save_upload_from_form().
151 if ($this->state->get('file_test.disable_error_collection')) {
152 define('SIMPLETEST_COLLECT_ERRORS', FALSE);
155 $form['file_test_upload']['#upload_validators'] = $validators;
156 $form['file_test_upload']['#upload_location'] = $destination;
158 $this->messenger->addStatus($this->t('Number of error messages before _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));
159 $file = _file_save_upload_from_form($form['file_test_upload'], $form_state, 0, $form_state->getValue('file_test_replace'));
160 $this->messenger->addStatus($this->t('Number of error messages after _file_save_upload_from_form(): @count.', ['@count' => count($this->messenger->messagesByType(MessengerInterface::TYPE_ERROR))]));
163 $form_state->setValue('file_test_upload', $file);
164 $this->messenger->addStatus($this->t('File @filepath was uploaded.', ['@filepath' => $file->getFileUri()]));
165 $this->messenger->addStatus($this->t('File name is @filename.', ['@filename' => $file->getFilename()]));
166 $this->messenger->addStatus($this->t('File MIME type is @mimetype.', ['@mimetype' => $file->getMimeType()]));
167 $this->messenger->addStatus($this->t('You WIN!'));
169 elseif ($file === FALSE) {
170 $this->messenger->addError($this->t('Epic upload FAIL!'));
177 public function submitForm(array &$form, FormStateInterface $form_state) {}