public function CmisBrowserDocumentUploadForm::submitForm in CMIS API 8.2
Same name and namespace in other branches
- 8 src/Form/CmisBrowserDocumentUploadForm.php \Drupal\cmis\Form\CmisBrowserDocumentUploadForm::submitForm()
- 3.0.x src/Form/CmisBrowserDocumentUploadForm.php \Drupal\cmis\Form\CmisBrowserDocumentUploadForm::submitForm()
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides FormInterface::submitForm
File
- src/
Form/ CmisBrowserDocumentUploadForm.php, line 98
Class
- CmisBrowserDocumentUploadForm
- Provides a form to upload documents.
Namespace
Drupal\cmis\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$directory = $this->fileSystem
->getTempDirectory();
$requestFiles = $this
->getRequest()->files
->get('files')['local_file'];
$requestFilesPathname = $requestFiles
->getPathname();
$requestFilesName = $requestFiles
->getClientOriginalName();
$filename = $directory . '/' . $requestFilesName;
if (!is_uploaded_file($requestFilesPathname) || !copy($requestFilesPathname, $filename)) {
// Can't create file.
$this
->messenger()
->addWarning($this
->t('File can not be uploaded.'));
return;
}
// Open repository.
if ($repository = new CmisRepositoryController($values['config'], $values['folder_id'])) {
if (!empty($repository
->getBrowser()
->getConnection()
->validObjectName($requestFilesName, 'cmis:document', $values['folder_id']))) {
// Document exists. Delete file from local.
unlink($filename);
$this
->messenger()
->addWarning($this
->t('The document name @name exists in folder.', [
'@name' => $requestFilesName,
]));
return;
}
$session = $repository
->getBrowser()
->getConnection()
->getSession();
$properties = [
PropertyIds::OBJECT_TYPE_ID => 'cmis:document',
PropertyIds::NAME => $requestFilesName,
];
if (!empty($values['description'])) {
$properties[PropertyIds::DESCRIPTION] = $values['description'];
}
// Create document.
try {
$session
->createDocument($properties, $session
->createObjectId($values['folder_id']), Stream::factory(fopen($filename, 'r')));
// Delete file from local.
unlink($filename);
$this
->messenger()
->addStatus($this
->t('Document name @name has been created.', [
'@name' => $requestFilesName,
]));
} catch (Exception $exception) {
$this
->messenger()
->addWarning($this
->t('Document name @name could not be created.', [
'@name' => $requestFilesName,
]));
}
}
}