You are here

protected function ContentImporter::ensureFile in Commerce Demo 8

Same name and namespace in other branches
  1. 8.2 src/ContentImporter.php \Drupal\commerce_demo\ContentImporter::ensureFile()

Ensures the existence of a file.

Parameters

string $filename: The filename. Assumed to exist in the content/files module subdirectory.

Return value

\Drupal\file\FileInterface The file.

1 call to ContentImporter::ensureFile()
ContentImporter::importEntity in src/ContentImporter.php
Imports a given entity.

File

src/ContentImporter.php, line 349

Class

ContentImporter
Defines the content importer.

Namespace

Drupal\commerce_demo

Code

protected function ensureFile($filename) {
  $file_storage = $this->entityTypeManager
    ->getStorage('file');
  $files = $file_storage
    ->loadByProperties([
    'filename' => $filename,
  ]);
  $file = reset($files);
  if (!$file) {
    $path = $this->contentPath . '/files/' . $filename;
    $uri = $this->fileSystem
      ->copy($path, 'public://' . $filename, FileSystemInterface::EXISTS_REPLACE);
    $file = $file_storage
      ->create([
      'filename' => $filename,
      'uri' => $uri,
      'status' => 1,
    ]);
    $file
      ->save();
  }
  return $file;
}