You are here

private function Presentation::createUnsortedRowsAndColumns in Filebrowser 3.x

Same name and namespace in other branches
  1. 8.2 src/Presentation.php \Drupal\filebrowser\Presentation::createUnsortedRowsAndColumns()
1 call to Presentation::createUnsortedRowsAndColumns()
Presentation::listView in src/Presentation.php

File

src/Presentation.php, line 304

Class

Presentation

Namespace

Drupal\filebrowser

Code

private function createUnsortedRowsAndColumns(&$unsorted_rows, &$visible_columns, $column_names, &$directory_empty) {
  $hide_extension = $this->filebrowser->hideExtension;
  $selected_columns = $this->filebrowser->visibleColumns;
  $files = $this->dbFileList['files'];
  foreach ($files as $file_name => $file) {

    /** @var DisplayFile $file */

    // Skip dot folder
    if ($file_name == ".") {
      continue;
    }

    // At least one file
    $directory_empty = false;

    // File extension case
    if ($file_name != ".." && $file->fileData->type == 'file' && $hide_extension) {
      $pos = strrpos($file->displayName, ".");
      $data[Common::NAME] = substr($file->displayName, 0, $pos);
    }

    // if the list has actions we will add the file id for creation of the select boxes
    if (!empty($this->formActions)) {
      $unsorted_rows[$file_name]['fid'] = $file->fileData->mimetype != 'folder/parent' ? "{$file->fid}" : '';
    }
    if (!empty($selected_columns[Common::ICON])) {

      // ##### ICON COLUMN #####
      $visible_columns[Common::ICON] = true;
      $icon = $this->common
        ->iconGenerate($file->fileData->type, $file->fileData->mimetype, 24, 24);
      $unsorted_rows[$file_name][Common::ICON] = render($icon);
    }

    // ##### NAME COLUMN - we will always set the name column
    // fixme: delete not needed
    $unsorted_rows[$file_name][Common::NAME] = render($file->link);
    $visible_columns[Common::NAME] = true;

    // ##### SET OTHER METADATA
    // for each file we will loop trough the column names.
    // if it is selected ($selected_columns) and contains data we will
    // add them to the visible array in $unsorted_rows
    $query = \Drupal::entityQuery('filebrowser_metadata_entity')
      ->condition('fid', $file->fid);
    $ids = $query
      ->execute();
    $metadata_all = \Drupal::entityTypeManager()
      ->getStorage('filebrowser_metadata_entity')
      ->loadMultiple($ids);

    /** @var FilebrowserMetadataEntity $metadata */
    foreach ($metadata_all as $metadata) {
      $name = $metadata
        ->getName();
      if ($selected_columns[$name]) {
        $visible_columns[$name] = true;
        $content = unserialize($metadata->content->value);
        $theme = $metadata->theme->value;
        if (is_null($theme)) {
          $unsorted_rows[$file_name][$name] = $content;
        }
        else {
          $render = [];
          $render['#theme'] = $theme;
          $render['#data'] = $content;
          $unsorted_rows[$file_name][$name] = render($render);
        }
      }
    }
  }
}