Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / file / tests / file_test / src / Form / FileTestSaveUploadFromForm.php
1 <?php
2
3 namespace Drupal\file_test\Form;
4
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;
10
11 /**
12  * File test form class.
13  */
14 class FileTestSaveUploadFromForm extends FormBase {
15
16   /**
17    * Stores the state storage service.
18    *
19    * @var \Drupal\Core\State\StateInterface
20    */
21   protected $state;
22
23   /**
24    * The messenger.
25    *
26    * @var \Drupal\Core\Messenger\MessengerInterface
27    */
28   protected $messenger;
29
30   /**
31    * Constructs a FileTestSaveUploadFromForm object.
32    *
33    * @param \Drupal\Core\State\StateInterface $state
34    *   The state key value store.
35    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
36    *   The messenger.
37    */
38   public function __construct(StateInterface $state, MessengerInterface $messenger) {
39     $this->state = $state;
40     $this->messenger = $messenger;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container) {
47     return new static(
48       $container->get('state'),
49       $container->get('messenger')
50     );
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function getFormId() {
57     return '_file_test_save_upload_from_form';
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function buildForm(array $form, FormStateInterface $form_state) {
64     $form['file_test_upload'] = [
65       '#type' => 'file',
66       '#multiple' => TRUE,
67       '#title' => $this->t('Upload a file'),
68     ];
69     $form['file_test_replace'] = [
70       '#type' => 'select',
71       '#title' => $this->t('Replace existing image'),
72       '#options' => [
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'),
76       ],
77       '#default_value' => FILE_EXISTS_RENAME,
78     ];
79     $form['file_subdir'] = [
80       '#type' => 'textfield',
81       '#title' => $this->t('Subdirectory for test file'),
82       '#default_value' => '',
83     ];
84
85     $form['extensions'] = [
86       '#type' => 'textfield',
87       '#title' => $this->t('Allowed extensions.'),
88       '#default_value' => '',
89     ];
90
91     $form['allow_all_extensions'] = [
92       '#type' => 'checkbox',
93       '#title' => $this->t('Allow all extensions?'),
94       '#default_value' => FALSE,
95     ];
96
97     $form['is_image_file'] = [
98       '#type' => 'checkbox',
99       '#title' => $this->t('Is this an image file?'),
100       '#default_value' => TRUE,
101     ];
102
103     $form['error_message'] = [
104       '#type' => 'textfield',
105       '#title' => $this->t('Custom error message.'),
106       '#default_value' => '',
107     ];
108
109     $form['submit'] = [
110       '#type' => 'submit',
111       '#value' => $this->t('Submit'),
112     ];
113     return $form;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
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);
125     }
126     else {
127       $destination = FALSE;
128     }
129
130     // Preset custom error message if requested.
131     if ($form_state->getValue('error_message')) {
132       $this->messenger->addError($form_state->getValue('error_message'));
133     }
134
135     // Setup validators.
136     $validators = [];
137     if ($form_state->getValue('is_image_file')) {
138       $validators['file_validate_is_image'] = [];
139     }
140
141     if ($form_state->getValue('allow_all_extensions')) {
142       $validators['file_validate_extensions'] = [];
143     }
144     elseif (!$form_state->isValueEmpty('extensions')) {
145       $validators['file_validate_extensions'] = [$form_state->getValue('extensions')];
146     }
147
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);
153     }
154
155     $form['file_test_upload']['#upload_validators'] = $validators;
156     $form['file_test_upload']['#upload_location'] = $destination;
157
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))]));
161
162     if ($file) {
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!'));
168     }
169     elseif ($file === FALSE) {
170       $this->messenger->addError($this->t('Epic upload FAIL!'));
171     }
172   }
173
174   /**
175    * {@inheritdoc}
176    */
177   public function submitForm(array &$form, FormStateInterface $form_state) {}
178
179 }