You are here

public function ServiceAuditFilesReferencedNotUsed::auditfilesReferencedNotUsedBatchAddProcessFile in Audit Files 8.3

Same name and namespace in other branches
  1. 8.2 src/ServiceAuditFilesReferencedNotUsed.php \Drupal\auditfiles\ServiceAuditFilesReferencedNotUsed::auditfilesReferencedNotUsedBatchAddProcessFile()
  2. 4.x src/ServiceAuditFilesReferencedNotUsed.php \Drupal\auditfiles\ServiceAuditFilesReferencedNotUsed::auditfilesReferencedNotUsedBatchAddProcessFile()

Adds the specified file to the file_usage table.

Parameters

string $reference_id: The ID for keeping track of the reference.

File

src/ServiceAuditFilesReferencedNotUsed.php, line 244

Class

ServiceAuditFilesReferencedNotUsed
List all methods used in referenced not used functionality.

Namespace

Drupal\auditfiles

Code

public function auditfilesReferencedNotUsedBatchAddProcessFile($reference_id) {
  $reference_id_parts = explode('.', $reference_id);
  $connection = $this->connection;
  $data = [
    'fid' => $reference_id_parts[4],
    // @todo This is hard coded for now, but need to determine how to figure out
    // which module needs to be here.
    'module' => 'file',
    'type' => $reference_id_parts[3],
    'id' => $reference_id_parts[2],
    'count' => 1,
  ];

  // Make sure the file is not already in the database.
  $query = 'SELECT fid FROM {file_usage}
    WHERE fid = :fid AND module = :module AND type = :type AND id = :id';
  $existing_file = $connection
    ->query($query, [
    ':fid' => $data['fid'],
    ':module' => $data['module'],
    ':type' => $data['type'],
    ':id' => $data['id'],
  ])
    ->fetchAll();
  if (empty($existing_file)) {

    // The file is not already in the database, so add it.
    $connection
      ->insert('file_usage')
      ->fields($data)
      ->execute();
  }
  else {
    $this
      ->messenger()
      ->addError($this->stringTranslation
      ->translate('The file is already in the file_usage table (file id: "@fid", module: "@module", type: "@type", entity id: "@id").', [
      '@fid' => $data['fid'],
      '@module' => $data['module'],
      '@type' => $data['type'],
      '@id' => $data['id'],
    ]));
  }
}