You are here

public function ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileData in Audit Files 8.2

Same name and namespace in other branches
  1. 8.3 src/ServiceAuditFilesUsedNotReferenced.php \Drupal\auditfiles\ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileData()
  2. 4.x src/ServiceAuditFilesUsedNotReferenced.php \Drupal\auditfiles\ServiceAuditFilesUsedNotReferenced::auditfilesUsedNotReferencedGetFileData()

Retrieves information about an individual file from the database.

Parameters

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

Return value

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

File

src/ServiceAuditFilesUsedNotReferenced.php, line 131

Class

ServiceAuditFilesUsedNotReferenced
List all methods used in files used not managed functionality.

Namespace

Drupal\auditfiles

Code

public function auditfilesUsedNotReferencedGetFileData($file_id) {
  $connection = $this->connection;
  $file_managed = $connection
    ->query("SELECT * FROM {file_managed} fm WHERE fid = {$file_id}")
    ->fetchObject();
  if (empty($file_managed)) {
    $url = Url::fromUri('internal:/admin/reports/auditfiles/usednotmanaged');
    $result_link = Link::fromTextAndUrl($this->stringTranslation
      ->translate('Used not managed'), $url)
      ->toString();
    $row = [
      'fid' => $this->stringTranslation
        ->translate('This file is not listed in the file_managed table. See the "%usednotmanaged" report.', [
        '%usednotmanaged' => $result_link,
      ]),
      'uri' => '',
      'usage' => '',
    ];
  }
  else {
    $usage = '<ul>';
    $results = $connection
      ->query("SELECT * FROM {file_usage} WHERE fid = {$file_id}");
    foreach ($results as $file_usage) {
      $used_by = $file_usage->module;
      $type = $file_usage->type;
      $url = Url::fromUri('internal:/node/' . $file_usage->id);
      $result_link = Link::fromTextAndUrl($file_usage->id, $url)
        ->toString();
      $used_in = $file_usage->type == 'node' ? $result_link : $file_usage->id;
      $times_used = $file_usage->count;
      $usage .= '<li>' . $this->stringTranslation
        ->translate('Used by module: %used_by, as object type: %type, in content ID: %used_in; Times used: %times_used', [
        '%used_by' => $used_by,
        '%type' => $type,
        '%used_in' => $used_in,
        '%times_used' => $times_used,
      ]) . '</li>';
    }
    $usage .= '</ul>';
    $usage = new FormattableMarkup($usage, []);
    $row = [
      'fid' => $file_id,
      'uri' => $file_managed->uri,
      'usage' => $usage,
    ];
  }
  return $row;
}