function Exporter::exportToFile
Exports an entity to a YAML file in a directory.
Any attachments to the entity (e.g., physical files) will be copied into the destination directory, alongside the exported entity.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to export.
string $destination: A destination path or URI; will be created if it does not exist. A subdirectory will be created for the entity type that is being exported.
Return value
\Drupal\Core\DefaultContent\ExportResult The exported entity data and its metadata.
File
-
core/
lib/ Drupal/ Core/ DefaultContent/ Exporter.php, line 123
Class
- Exporter
- Handles exporting content entities.
Namespace
Drupal\Core\DefaultContentCode
public function exportToFile(ContentEntityInterface $entity, string $destination) : ExportResult {
$destination .= '/' . $entity->getEntityTypeId();
// Ensure the destination directory exists and is writable.
$this->fileSystem
->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS) || throw new DirectoryNotReadyException("Could not create destination directory '{$destination}'");
$destination = $this->fileSystem
->realpath($destination);
if (empty($destination)) {
throw new FileException("Could not resolve the destination directory '{$destination}'");
}
$path = $destination . '/' . $entity->uuid() . '.' . Yaml::getFileExtension();
$result = $this->export($entity);
file_put_contents($path, (string) $result) || throw new FileWriteException("Could not write file '{$path}'");
foreach ($result->metadata
->getAttachments() as $from => $to) {
$this->fileSystem
->copy($from, $destination . '/' . $to, FileExists::Replace);
}
return $result;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.