You are here

public static function TaxonomyManagerTree::loadTerms in Taxonomy Manager 8

Same name and namespace in other branches
  1. 2.0.x src/Element/TaxonomyManagerTree.php \Drupal\taxonomy_manager\Element\TaxonomyManagerTree::loadTerms()

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

2 calls to TaxonomyManagerTree::loadTerms()
SubTreeController::json in src/Controller/SubTreeController.php
JSON callback for subtree.
TaxonomyManagerTree::processTree in src/Element/TaxonomyManagerTree.php

File

src/Element/TaxonomyManagerTree.php, line 88

Class

TaxonomyManagerTree
Taxonomy Manager Tree Form Element.

Namespace

Drupal\taxonomy_manager\Element

Code

public static function loadTerms($vocabulary, $parent = 0, $pager_size = -1) {
  try {
    $query = \Drupal::entityQuery('taxonomy_term')
      ->condition('vid', $vocabulary
      ->id())
      ->condition('parent', $parent)
      ->sort('weight')
      ->sort('name');
    if ($pager_size > 0) {
      $query
        ->pager($pager_size);
    }
    $tids = $query
      ->execute();
    return \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadMultiple($tids);
  } 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 taxonomy_manager module only supports Drupal 8.5 or
    // newer.
  }
  $database = \Drupal::database();
  if ($pager_size > 0) {
    $query = $database
      ->select('taxonomy_term_data', 'td')
      ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
  }
  else {
    $query = $database
      ->select('taxonomy_term_data', 'td');
  }
  $query
    ->fields('td', [
    'tid',
  ]);
  $query
    ->condition('td.vid', $vocabulary
    ->id());
  $query
    ->join('taxonomy_term__parent', 'th', 'td.tid = th.entity_id AND th.parent_target_id = :parent', [
    ':parent' => $parent,
  ]);
  $query
    ->join('taxonomy_term_field_data', 'tfd', 'td.tid = tfd.tid');
  $query
    ->orderBy('tfd.weight');
  $query
    ->orderBy('tfd.name');
  if ($pager_size > 0) {
    $query
      ->limit($pager_size);
  }
  $result = $query
    ->execute();
  $tids = [];
  foreach ($result as $record) {
    $tids[] = $record->tid;
  }
  return \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadMultiple($tids);
}