public function BlockUploadPluploadForm::submitForm in Block Upload 8
Saves files uploaded via plupload form.
Example taken from plupload_test_submit();
Overrides FormInterface::submitForm
File
- src/
Form/ BlockUploadPluploadForm.php, line 108
Class
- BlockUploadPluploadForm
- Block upload form.
Namespace
Drupal\block_upload\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$nid = $values['block_upload_nid'];
$buid = $values['buid'];
$field_name = explode('.', \Drupal::state()
->get('block_upload_' . $buid . '_field') ?: '')[1];
$node = Node::load($nid);
if (isset($values['remove_files'])) {
if (array_filter($values['remove_files'])) {
BlockUploadBuild::blockUploadDeleteFiles($node, $field_name, $values);
}
}
if (isset($values['block_upload_file'])) {
$uid = \Drupal::currentUser();
$destination = \Drupal::config('system.file')
->get('default_scheme') . '://plupload';
\Drupal::service('file_system')
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
foreach ($values['block_upload_file'] as $uploaded_file) {
$file_uri = StreamWrapperManagerInterface::normalizeUri($destination . '/' . $uploaded_file['name']);
// Move file without creating a new 'file' entity.
$uri = \Drupal::service('file_system')
->move($uploaded_file['tmppath'], $file_uri);
$file = File::Create([
'uri' => $uri,
'uid' => $uid
->id(),
]);
$file
->save();
// @todo: When https://www.drupal.org/node/2245927 is resolved,
// use a helper to save file to file_managed table
$node
->get($field_name)
->appendItem($file);
$this
->messenger()
->addMessage(t('File was successfully uploaded!'));
}
}
$node
->save();
}