You are here

function ServiceAuditFilesReferencedNotUsed::_auditfiles_referenced_not_used_get_file_data in Audit Files 8

Retrieves information about an individual file from the database.

Parameters

array $row_data: The data to use for creating the row.

Return value

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

File

src/ServiceAuditFilesReferencedNotUsed.php, line 80
providing the service that used in 'referenced not used' functionality.

Class

ServiceAuditFilesReferencedNotUsed

Namespace

Drupal\auditfiles

Code

function _auditfiles_referenced_not_used_get_file_data(array $row_data) {
  $config = \Drupal::config('auditfiles_config.settings');
  $connection = Database::getConnection();
  $query = 'SELECT * FROM {' . $row_data['table'] . '} WHERE ' . $row_data['column'] . ' = ' . $row_data['file_id'];
  $result = $connection
    ->query($query)
    ->fetchAll();
  $result = reset($result);
  if ($row_data['entity_type'] == 'node') {
    $url = Url::fromUri('internal:/node/' . $result->entity_id);
    $entity_id_display = Link::fromTextAndUrl('node/' . $result->entity_id, $url)
      ->toString();
  }
  else {
    $entity_id_display = $result->entity_id;
  }
  $row = [
    'file_id' => $result->{$row_data['column']},
    'entity_type' => $row_data['entity_type'],
    'bundle' => [
      'data' => $result->bundle,
      'hidden' => TRUE,
    ],
    'entity_id' => [
      'data' => $result->entity_id,
      'hidden' => TRUE,
    ],
    'entity_id_display' => $entity_id_display,
    'field' => $row_data['table'] . '.' . $row_data['column'],
    'table' => [
      'data' => $row_data['table'],
      'hidden' => TRUE,
    ],
    'uri' => 'No file object exists for this reference.',
    'filename' => [
      'data' => '',
      'hidden' => TRUE,
    ],
    'filemime' => '--',
    'filesize' => '--',
  ];

  // If there is a file in the file_managed table, add some of that
  // information to the row, too.
  $file_managed = File::load($result->{$row_data['column']});
  if (!empty($file_managed)) {
    $row['uri'] = $file_managed
      ->getFileuri();
    $row['filename'] = [
      'data' => $file_managed
        ->getFilename(),
      'hidden' => TRUE,
    ];
    $row['filemime'] = $file_managed
      ->getMimeType();
    $row['filesize'] = $file_managed
      ->getSize();
  }
  return $row;
}