You are here

public function ServiceAuditFilesMergeFileReferences::auditfilesMergeFileReferencesGetFileData in Audit Files 8.3

Same name and namespace in other branches
  1. 8.2 src/ServiceAuditFilesMergeFileReferences.php \Drupal\auditfiles\ServiceAuditFilesMergeFileReferences::auditfilesMergeFileReferencesGetFileData()

Retrieves information about an individual file from the database.

Parameters

int $file_name: The ID of the file to prepare for display.

string $date_format: The date format to prepair for display.

Return value

array The row for the table on the report, with the file's information formatted for display.

File

src/ServiceAuditFilesMergeFileReferences.php, line 128

Class

ServiceAuditFilesMergeFileReferences
Define all methods that used in merge file references functionality.

Namespace

Drupal\auditfiles

Code

public function auditfilesMergeFileReferencesGetFileData($file_name, $date_format) {
  $connection = $this->connection;
  $fid_query = 'SELECT fid FROM {file_managed} WHERE filename = :filename';
  $fid_results = $connection
    ->query($fid_query, [
    ':filename' => $file_name,
  ])
    ->fetchAll();
  if (count($fid_results) > 0) {
    $references = '<ul>';
    foreach ($fid_results as $fid_result) {
      $query = $connection
        ->select('file_managed', 'fm');
      $query
        ->condition('fm.fid', $fid_result->fid);
      $query
        ->fields('fm', [
        'fid',
        'uid',
        'filename',
        'uri',
        'filemime',
        'filesize',
        'created',
        'status',
      ]);
      $results = $query
        ->execute()
        ->fetchAll();
      $file = $results[0];
      $references .= '<li>' . $this->stringTranslation
        ->translate('<strong>Fid: </strong> %id , <strong>Name : </strong> %file , <strong>File URI: </strong> %uri , <strong>Date: </strong> %date', [
        '%id' => $file->fid,
        '%file' => $file->filename,
        '%uri' => $file->uri,
        '%date' => $this->dateFormatter
          ->format($file->created, $date_format),
      ]) . '</li>';
    }
    $references .= '</ul>';
    $usage = new FormattableMarkup($references, []);
    return [
      'filename' => $file_name,
      'references' => $usage,
    ];
  }
}