You are here

public function DatabaseFileUsageBackend::delete in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php \Drupal\file\FileUsage\DatabaseFileUsageBackend::delete()

Removes a record to indicate that a module is no longer using a file.

Parameters

\Drupal\file\FileInterface $file: A file entity.

string $module: The name of the module using the file.

string $type: (optional) The type of the object that contains the referenced file. May be omitted if all module references to a file are being deleted. Defaults to NULL.

int $id: (optional) The unique, numeric ID of the object containing the referenced file. May be omitted if all module references to a file are being deleted. Defaults to NULL.

int $count: (optional) The number of references to delete from the object. Defaults to 1. Zero may be specified to delete all references to the file within a specific object.

Overrides FileUsageBase::delete

File

core/modules/file/src/FileUsage/DatabaseFileUsageBackend.php, line 68
Contains \Drupal\file\FileUsage\DatabaseFileUsageBackend.

Class

DatabaseFileUsageBackend
Defines the database file usage backend. This is the default Drupal backend.

Namespace

Drupal\file\FileUsage

Code

public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1) {

  // Delete rows that have a exact or less value to prevent empty rows.
  $query = $this->connection
    ->delete($this->tableName)
    ->condition('module', $module)
    ->condition('fid', $file
    ->id());
  if ($type && $id) {
    $query
      ->condition('type', $type)
      ->condition('id', $id);
  }
  if ($count) {
    $query
      ->condition('count', $count, '<=');
  }
  $result = $query
    ->execute();

  // If the row has more than the specified count decrement it by that number.
  if (!$result && $count > 0) {
    $query = $this->connection
      ->update($this->tableName)
      ->condition('module', $module)
      ->condition('fid', $file
      ->id());
    if ($type && $id) {
      $query
        ->condition('type', $type)
        ->condition('id', $id);
    }
    $query
      ->expression('count', 'count - :count', array(
      ':count' => $count,
    ));
    $query
      ->execute();
  }
  parent::delete($file, $module, $type, $id, $count);
}