You are here

public function ContentFileStorage::writeEntity in Default Content for D8 2.0.x

Writes a normalized entity to the given folder.

Parameters

string $folder: The target folder.

string $encoded: The encoded entity (YAML).

\Drupal\Core\Entity\ContentEntityInterface $entity: The content being written.

string $filename: (optional) The name of the file, defaults to UUID.yml. Must end with .yml.

Overrides ContentFileStorageInterface::writeEntity

File

src/ContentFileStorage.php, line 67

Class

ContentFileStorage
Finds, reads and writes default content files.

Namespace

Drupal\default_content

Code

public function writeEntity(string $folder, string $encoded, ContentEntityInterface $entity, string $filename = NULL) {

  // Ensure that the folder per entity type exists.
  $entity_type_folder = "{$folder}/" . $entity
    ->getEntityTypeId();
  $this->fileSystem
    ->prepareDirectory($entity_type_folder, FileSystemInterface::CREATE_DIRECTORY);
  $filename = $filename ?: $entity
    ->uuid() . '.yml';
  file_put_contents($entity_type_folder . '/' . $filename, $encoded);

  // For files, copy the file into the same folder.
  if ($entity instanceof FileInterface) {
    $this->fileSystem
      ->copy($entity
      ->getFileUri(), $entity_type_folder . '/' . $entity
      ->getFilename(), FileSystemInterface::EXISTS_REPLACE);
  }
}