You are here

protected function BatchWatermarkForm::getImageFiles in Media watermark 8

Get files sortable table.

Return value

array Renderable array.

1 call to BatchWatermarkForm::getImageFiles()
BatchWatermarkForm::buildForm in src/Form/BatchWatermarkForm.php
Define the form used for ContentEntityExample settings.

File

src/Form/BatchWatermarkForm.php, line 217

Class

BatchWatermarkForm
Class BatchWatermarkForm.

Namespace

Drupal\media_watermark\Form

Code

protected function getImageFiles() {
  $admin_access = \Drupal::currentUser()
    ->hasPermission('administer files');
  $header = $this
    ->buildHeader();
  $result = $this
    ->getFiles($header);
  $files = File::loadMultiple(array_keys($result));
  $uids = [];
  foreach ($files as $file) {
    $uids[] = $file
      ->getOwnerId();
  }
  $accounts = !empty($uids) ? User::loadMultiple(array_unique($uids)) : [];

  // Prepare the list of files.
  $options = [];
  foreach ($files as $file) {
    $file_url = parse_url($file
      ->toUrl());
    $options[$file
      ->id()] = [
      'filename' => [
        'data' => [
          '#type' => 'link',
          '#title' => $file
            ->getFilename(),
          '#url' => Url::fromUserInput($file_url['path']),
          '#attributes' => [
            'target' => '_blank',
          ],
        ],
      ],
      'image_preview' => [
        'data' => [
          '#theme' => 'image_style',
          '#width' => 100,
          '#height' => NULL,
          '#style_name' => 'media_watermark',
          '#uri' => $file
            ->getFileUri(),
        ],
      ],
      'type' => 'image',
      'filesize' => format_size($file
        ->getSize()),
      'author' => [
        'data' => [
          '#type' => 'link',
          '#title' => is_object($accounts[$file
            ->getOwnerId()]) ? $accounts[$file
            ->getOwnerId()]
            ->getUsername() : 'Anonymous',
          '#url' => is_object($accounts[$file
            ->getOwnerId()]) ? Url::fromRoute('entity.user.canonical', [
            'user' => $file
              ->getOwnerId(),
          ]) : Url::fromUserInput('/user/0'),
          '#attributes' => [
            'target' => '_blank',
          ],
        ],
      ],
      'timestamp' => \Drupal::service('date.formatter')
        ->format($file
        ->getCreatedTime(), 'short'),
    ];

    // Show a warning for files that do not exist.
    if (@(!is_file($file
      ->getFileUri()))) {
      $options[$file
        ->id()]['#attributes']['class'][] = 'error';
      if ($file
        ->getFileUri()) {
        $options[$file
          ->id()]['#attributes']['title'] = t('The stream wrapper for @scheme files is missing.', [
          '@scheme' => FileSystem::uriScheme($file->uri),
        ]);
      }
      else {
        $options[$file
          ->id()]['#attributes']['title'] = t('The file does not exist.');
      }
    }
  }

  // Only use a tableselect when the current user is able to perform any
  // operations.
  if ($admin_access) {
    $form['files'] = [
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => t('No files available.'),
      '#multiple' => TRUE,
    ];
  }
  else {
    $form['files'] = [
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $options,
      '#empty' => t('No files available.'),
    ];
  }
  $form['pager'] = [
    '#type' => 'pager',
  ];
  return $form;
}