You are here

function _tft_folder_content in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x tft.module \_tft_folder_content()

Returns folder content.

Parameters

int $tid: The taxonomy term tid.

Return value

array The folder content

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to _tft_folder_content()
TFTController::get_content in src/Controller/TFTController.php
Returns folder content.
_tft_folder_tree in ./tft.module
Returns TFT folder tree.

File

./tft.module, line 245
Contains tft.module.

Code

function _tft_folder_content($tid, $only_terms = FALSE, $gid = NULL) {
  $content = [];

  /** @var \Drupal\taxonomy\TermStorage $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $result = $storage
    ->loadTree('tft_tree', $tid, 1);
  array_walk($result, function ($term) use (&$content) {
    $content[] = [
      'id' => $term->tid,
      'type' => 'term',
      'name' => $term->name,
      'weight' => $term->weight,
    ];
  });
  if ($only_terms) {
    return $content;
  }

  // Get the files.
  $fids = \Drupal::entityQuery('media')
    ->condition('bundle', 'tft_file')
    ->condition('tft_folder.target_id', $tid)
    ->execute();
  $files = Media::loadMultiple($fids);
  $user = \Drupal::currentUser();
  $user_id = $user
    ->id();
  array_walk($files, function ($file) use ($user_id, &$content) {

    /** @var \Drupal\media\Entity\Media $file */
    if ($file
      ->hasField('tft_members')) {
      $members = $file
        ->get('tft_members')
        ->getValue();
      if (!empty($members)) {
        $members = array_map(function ($member) {
          return $member['target_id'];
        }, $members);
        if (!in_array($user_id, $members)) {
          return;
        }
      }
    }
    $content[] = [
      'id' => $file
        ->id(),
      'type' => 'file',
      'name' => $file
        ->getName(),
    ];
  });
  return $content;
}