function FileRepository::move
Same name in other branches
- 9 core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::move()
- 11.x core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::move()
File
-
core/
modules/ file/ src/ FileRepository.php, line 176
Class
- FileRepository
- Provides a file entity repository.
Namespace
Drupal\fileCode
public function move(FileInterface $source, string $destination, FileExists|int $fileExists = FileExists::Rename) : FileInterface {
if (!$fileExists instanceof FileExists) {
// @phpstan-ignore-next-line
$fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
}
if (!$this->streamWrapperManager
->isValidUri($destination)) {
throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
}
$uri = $this->fileSystem
->move($source->getFileUri(), $destination, $fileExists);
$delete_source = FALSE;
$file = clone $source;
$file->setFileUri($uri);
// If we are replacing an existing file re-use its database record.
if ($fileExists === FileExists::Replace) {
if ($existing = $this->loadByUri($uri)) {
$delete_source = TRUE;
$file->fid = $existing->id();
$file->uuid = $existing->uuid();
}
}
elseif ($fileExists === FileExists::Rename && is_file($destination)) {
$file->setFilename($this->fileSystem
->basename($destination));
}
$file->save();
// Inform modules that the file has been moved.
$this->moduleHandler
->invokeAll('file_move', [
$file,
$source,
]);
// Delete the original if it's not in use elsewhere.
if ($delete_source && !$this->fileUsage
->listUsage($source)) {
$source->delete();
}
return $file;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.