You are here

public static function TermReferenceFancytree::loadTerms in Term Reference Fancytree 8.2

Same name and namespace in other branches
  1. 8 src/Element/TermReferenceFancytree.php \Drupal\term_reference_fancytree\Element\TermReferenceFancytree::loadTerms()

Load one single level of terms, sorted by weight and alphabet.

3 calls to TermReferenceFancytree::loadTerms()
SubTreeController::json in src/Controller/SubTreeController.php
JSON callback for subtree.
TermReferenceFancytree::getTopLevelNodes in src/Element/TermReferenceFancytree.php
Function that returns the top level nodes for the tree.
TermReferenceFancytree::getVocabularyNamesJsonArray in src/Element/TermReferenceFancytree.php
Function that generates a list of vocabulary names in JSON.

File

src/Element/TermReferenceFancytree.php, line 173

Class

TermReferenceFancytree
Term Reference Tree Form Element.

Namespace

Drupal\term_reference_fancytree\Element

Code

public static function loadTerms($vocabulary, $parent = 0) {
  try {
    $query = \Drupal::entityQuery('taxonomy_term')
      ->condition('vid', $vocabulary
      ->id())
      ->condition('parent', $parent)
      ->sort('weight')
      ->sort('name');
    $tids = $query
      ->execute();
    $terms = TermReferenceFancytree::getTermStorage()
      ->loadMultiple($tids);
    $language = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
    foreach ($terms as $tid => $term) {
      if ($term
        ->hasTranslation($language)) {
        $terms[$tid] = $term
          ->getTranslation($language);
      }
    }
    return $terms;
  } catch (QueryException $e) {

    // This site is still using the pre-Drupal 8.5 database schema, where
    // https://www.drupal.org/project/drupal/issues/2543726 was not yet
    // committed to Drupal core.
    // @todo Remove both the try/catch wrapper and the code below the catch-
    // statement once the module only supports Drupal 8.5 or
    // newer.
  }
  $database = \Drupal::database();
  $query = $database
    ->select('taxonomy_term_data', 'td');
  $query
    ->fields('td', [
    'tid',
  ]);
  $query
    ->condition('td.vid', $vocabulary
    ->id());
  $query
    ->join('taxonomy_term_hierarchy', 'th', 'td.tid = th.tid AND th.parent = :parent', [
    ':parent' => $parent,
  ]);
  $query
    ->join('taxonomy_term_field_data', 'tfd', 'td.tid = tfd.tid');
  $query
    ->orderBy('tfd.weight');
  $query
    ->orderBy('tfd.name');
  $result = $query
    ->execute();
  $tids = [];
  foreach ($result as $record) {
    $tids[] = $record->tid;
  }
  return \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadMultiple($tids);
}