function Importer::copyFileAssociatedWithEntity
Same name in other branches
- 11.x core/lib/Drupal/Core/DefaultContent/Importer.php \Drupal\Core\DefaultContent\Importer::copyFileAssociatedWithEntity()
Copies a file from default content directory to the site's file system.
Parameters
string $path: The path to the file to copy.
\Drupal\file\FileInterface $entity: The file entity.
1 call to Importer::copyFileAssociatedWithEntity()
- Importer::importContent in core/
lib/ Drupal/ Core/ DefaultContent/ Importer.php - Imports content entities from disk.
File
-
core/
lib/ Drupal/ Core/ DefaultContent/ Importer.php, line 131
Class
- Importer
- A service for handling import of content.
Namespace
Drupal\Core\DefaultContentCode
private function copyFileAssociatedWithEntity(string $path, FileInterface &$entity) : void {
$destination = $entity->getFileUri();
assert(is_string($destination));
// If the source file doesn't exist, there's nothing we can do.
$source = $path . '/' . basename($destination);
if (!file_exists($source)) {
$this->logger?->warning("File entity %name was imported, but the associated file (@path) was not found.", [
'%name' => $entity->label(),
'@path' => $source,
]);
return;
}
$copy_file = TRUE;
if (file_exists($destination)) {
$source_hash = hash_file('sha256', $source);
assert(is_string($source_hash));
$destination_hash = hash_file('sha256', $destination);
assert(is_string($destination_hash));
if (hash_equals($source_hash, $destination_hash) && $this->entityTypeManager
->getStorage('file')
->loadByProperties([
'uri' => $destination,
]) === []) {
// If the file hashes match and the file is not already a managed file
// then do not copy a new version to the file system. This prevents
// re-installs during development from creating unnecessary duplicates.
$copy_file = FALSE;
}
}
$target_directory = dirname($destination);
$this->fileSystem
->prepareDirectory($target_directory, FileSystemInterface::CREATE_DIRECTORY);
if ($copy_file) {
$uri = $this->fileSystem
->copy($source, $destination);
$entity->setFileUri($uri);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.