You are here

public static function FileManagement::getFileInformation in File Management 8

Generates a render array with file information from the given file entity.

Parameters

\Drupal\file\FileInterface $file: The file entity from which to get the information from.

Return value

array The render array with the file information.

File

src/FileManagement.php, line 24

Class

FileManagement
File Management helper methods.

Namespace

Drupal\file_management

Code

public static function getFileInformation(FileInterface $file) {

  // Build a link to the file.
  $link = Link::fromTextAndUrl($file
    ->getFilename(), Url::fromUri(file_create_url($file
    ->getFileUri())))
    ->toRenderable();
  $link = \Drupal::service('renderer')
    ->render($link);

  // Get owner information.
  $owner = [
    '#theme' => 'username',
    '#account' => $file
      ->getOwner(),
  ];
  $owner = \Drupal::service('renderer')
    ->render($owner);

  // Build the render array.
  $file_information = [];
  if (static::isImage($file)) {
    $image = [
      '#theme' => 'image',
      '#uri' => $file
        ->getFileUri(),
    ];
    $image = \Drupal::service('renderer')
      ->render($image);
    $file_information[] = [
      '#type' => 'item',
      '#title' => t('Image'),
      '#markup' => $image,
    ];
  }
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('Filename'),
    '#markup' => $link,
  ];
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('File URI'),
    '#markup' => $file
      ->getFileUri(),
  ];
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('File MIME Type'),
    '#markup' => $file
      ->getMimeType(),
  ];
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('File size'),
    '#markup' => format_size($file
      ->getSize()),
  ];
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('Owner'),
    '#markup' => $owner,
  ];
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('Created'),
    '#markup' => \Drupal::service('date.formatter')
      ->format($file
      ->getCreatedTime()),
  ];
  $file_information[] = [
    '#type' => 'item',
    '#title' => t('Changed'),
    '#markup' => \Drupal::service('date.formatter')
      ->format($file
      ->getChangedTime()),
  ];
  return $file_information;
}