public function WebformManagedFileBase::addFiles in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Plugin/WebformElement/WebformManagedFileBase.php \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::addFiles()
Add a webform submission file's usage and mark it as permanent.
Parameters
array $element: An element.
\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.
array $fids: An array of file ids.
1 call to WebformManagedFileBase::addFiles()
- WebformManagedFileBase::postSave in src/
Plugin/ WebformElement/ WebformManagedFileBase.php - Acts on a saved webform submission element before the insert or update hook is invoked.
File
- src/
Plugin/ WebformElement/ WebformManagedFileBase.php, line 1191
Class
- WebformManagedFileBase
- Provides a base class webform 'managed_file' elements.
Namespace
Drupal\webform\Plugin\WebformElementCode
public function addFiles(array $element, WebformSubmissionInterface $webform_submission, array $fids) {
// Make sure the file.module is enabled since this method is called from
// \Drupal\webform\Plugin\WebformElement\WebformCompositeBase::postSave.
if (!$this->moduleHandler
->moduleExists('file')) {
return;
}
// Make sure there are files that need to added.
if (empty($fids)) {
return;
}
/** @var \Drupal\file\FileInterface[] $files */
$files = $this
->getFileStorage()
->loadMultiple($fids);
foreach ($files as $file) {
$source_uri = $file
->getFileUri();
$destination_uri = $this
->getFileDestinationUri($element, $file, $webform_submission);
// Save file if there is a new destination URI.
if ($source_uri !== $destination_uri) {
$destination_uri = $this->fileSystem
->move($source_uri, $destination_uri);
$file
->setFileUri($destination_uri);
$file
->setFileName($this->fileSystem
->basename($destination_uri));
$file
->save();
$this->entityTypeManager
->getStorage('file')
->resetCache([
$file
->id(),
]);
}
// Update file usage table.
// Setting file usage will also make the file's status permanent.
$this->fileUsage
->delete($file, 'webform', 'webform_submission', $webform_submission
->id());
$this->fileUsage
->add($file, 'webform', 'webform_submission', $webform_submission
->id());
}
}