You are here

public function MongodbFileUsage::delete in MongoDB 8

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.

string $id: (optional) The unique 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

src/MongodbFileUsage.php, line 64
Definition of Drupal\mongodb\MongodbFileUsage.

Class

MongodbFileUsage
Defines the mongodb file usage backend.

Namespace

Drupal\mongodb

Code

public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1) {
  $key = array(
    'fid' => (int) $file
      ->id(),
    'module' => $module,
  );
  if ($type && $id) {
    $key['type'] = $type;
    $key['id'] = (int) $id;
  }
  if ($count) {
    $key['count']['$lte'] = $count;
  }

  // Delete entries that have a exact or less value to prevent empty rows.
  $record = $this->database
    ->get($this->collection)
    ->remove($key, array(
    'safe' => TRUE,
  ));
  if (empty($record['n']) && $count > 0) {
    unset($key['count']);

    // Assume that we do not want to update if item is not in the collection.
    try {
      $this->database
        ->get($this->collection)
        ->update($key, array(
        '$inc' => array(
          'count' => -1 * $count,
        ),
      ), array(
        'safe' => TRUE,
      ));
    } catch (Exception $e) {
    }
  }
  parent::delete($file, $module, $type, $id, $count);
}