You are here

function ServiceAuditFilesUsedNotReferenced::_auditfiles_used_not_referenced_get_file_data in Audit Files 8

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 70
providing the service that used in 'used not referenced' functionality.

Class

ServiceAuditFilesUsedNotReferenced

Namespace

Drupal\auditfiles

Code

function _auditfiles_used_not_referenced_get_file_data($file_id) {
  $connection = Database::getConnection();
  $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(t('Used not managed'), $url)
      ->toString();
    $row = [
      'fid' => t('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>' . t('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;
}