class FileRepository implements FileRepositoryInterface (View source)

Provides a file entity repository.

Properties

protected FileSystemInterface $fileSystem

The file system service.

protected StreamWrapperManagerInterface $streamWrapperManager

The stream wrapper manager.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager.

protected ModuleHandlerInterface $moduleHandler

The module handler.

protected FileUsageInterface $fileUsage

The file usage service.

protected AccountInterface $currentUser

The current user.

Methods

__construct(FileSystemInterface $fileSystem, StreamWrapperManagerInterface $streamWrapperManager, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, FileUsageInterface $fileUsage, AccountInterface $currentUser)

FileRepository constructor.

writeData(string $data, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME)

Writes a file to the specified destination and creates a file entity.

createOrUpdate(string $uri, string $destination, bool $rename)

Create a file entity or update if it exists.

copy(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME)

Copies a file to a new location and adds a file record to the database.

move(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME)

Moves a file to a new location and update the file's database entry.

FileInterface|null
loadByUri(string $uri)

Loads the first File entity found with the specified URI.

Details

__construct(FileSystemInterface $fileSystem, StreamWrapperManagerInterface $streamWrapperManager, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, FileUsageInterface $fileUsage, AccountInterface $currentUser)

FileRepository constructor.

Parameters

FileSystemInterface $fileSystem

The file system.

StreamWrapperManagerInterface $streamWrapperManager

The stream wrapper manager.

EntityTypeManagerInterface $entityTypeManager

The entity type manager.

ModuleHandlerInterface $moduleHandler

The module handler.

FileUsageInterface $fileUsage

The file usage service.

AccountInterface $currentUser

The current user.

FileInterface writeData(string $data, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME)

Writes a file to the specified destination and creates a file entity.

Parameters

string $data

A string containing the contents of the file.

string $destination

A string containing the destination URI. This must be a stream wrapper URI.

int $replace

(optional) The replace behavior when the destination file already exists. Possible values include:

  • FileSystemInterface::EXISTSRENAME: (default) Append {incrementing number} until the filename is unique.
  • FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a managed file with the destination name exists, then its database entry will be updated. If no database entry is found, then a new one will be created.
  • FileSystemInterface::EXISTS_ERROR: Do nothing and throw a \Drupal\Core\File\Exception\FileExistsException.

Return Value

FileInterface

The file entity.

Exceptions

FileException
FileExistsException
InvalidStreamWrapperException
EntityStorageException

protected FileInterface createOrUpdate(string $uri, string $destination, bool $rename)

Create a file entity or update if it exists.

Parameters

string $uri

The file URI.

string $destination

The destination URI.

bool $rename

Whether to rename the file.

Return Value

FileInterface

The file entity.

Exceptions

EntityStorageException

FileInterface copy(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME)

Copies a file to a new location and adds a file record to the database.

This function should be used when manipulating files that have records stored in the database. This is a powerful function that in many ways performs like an advanced version of copy().

  • Checks if $source and $destination are valid and readable/writable.
  • If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
  • If the $source and $destination are equal, the behavior depends on the $replace parameter. FileSystemInterface::EXISTS_REPLACE will error out. FileSystemInterface::EXISTS_RENAME will rename the file until the $destination is unique.
  • Adds the new file to the files database. If the source file is a temporary file, the resulting file will also be a temporary file. See file_save_upload() for details on temporary files.

Parameters

FileInterface $source

A file entity.

string $destination

A string containing the destination that $source should be copied to. This must be a stream wrapper URI.

int $replace

(optional) Replace behavior when the destination file already exists. Possible values include:

  • FileSystemInterface::EXISTSRENAME: (default) Append {incrementing number} until the filename is unique.
  • FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a managed file with the destination name exists, then its database entry will be updated. If no database entry is found, then a new one will be created.
  • FileSystemInterface::EXISTS_ERROR: Do nothing and throw a \Drupal\Core\File\Exception\FileExistsException.

Return Value

FileInterface

The file entity.

Exceptions

FileException
FileExistsException
InvalidStreamWrapperException
EntityStorageException

FileInterface move(FileInterface $source, string $destination, int $replace = FileSystemInterface::EXISTS_RENAME)

Moves a file to a new location and update the file's database entry.

  • Checks if $source and $destination are valid and readable/writable.
  • Performs a file move if $source is not equal to $destination.
  • If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
  • Adds the new file to the files database.

Parameters

FileInterface $source

A file entity.

string $destination

A string containing the destination that $source should be moved to. This must be a stream wrapper URI.

int $replace

(optional) The replace behavior when the destination file already exists. Possible values include:

  • FileSystemInterface::EXISTSRENAME: (default) Append {incrementing number} until the filename is unique.
  • FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a managed file with the destination name exists then its database entry will be updated and $source->delete() called after invoking hook_file_move(). If no database entry is found, then the source files record will be updated.
  • FileSystemInterface::EXISTS_ERROR: Do nothing and throw a \Drupal\Core\File\Exception\FileExistsException.

Return Value

FileInterface

The file entity.

Exceptions

FileException
FileExistsException
InvalidStreamWrapperException
EntityStorageException

FileInterface|null loadByUri(string $uri)

Loads the first File entity found with the specified URI.

Parameters

string $uri

The file URI.

Return Value

FileInterface|null

The first file with the matched URI if found, NULL otherwise.