You are here

private function FaqController::_getIndentedFaqTerms in Frequently Asked Questions 8

Return a structured array that consists a list of terms indented according to the term depth.

Parameters

$vid: Vocabulary id.

$tid: Term id.

Return value

Return an array of a list of terms indented according to the term depth.

1 call to FaqController::_getIndentedFaqTerms()
FaqController::faqPage in src/Controller/FaqController.php
Function to display the faq page.

File

src/Controller/FaqController.php, line 473

Class

FaqController
Controller routines for FAQ routes.

Namespace

Drupal\faq\Controller

Code

private function _getIndentedFaqTerms($vid, $tid) {

  // If ($this->moduleHandler()->moduleExists('pathauto')) {
  // pathauto does't exists in D8 yet
  // }.
  $faq_settings = $this->config
    ->get('faq.settings');
  $display_faq_count = $faq_settings
    ->get('count');
  $hide_child_terms = $faq_settings
    ->get('hide_child_terms');
  $items = array();
  $tree = $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadTree($vid, $tid, 1, TRUE);
  foreach ($tree as $term) {
    $term_id = $term
      ->id();
    $tree_count = FaqHelper::taxonomyTermCountNodes($term_id);
    if ($tree_count) {

      // Get term description.
      $desc = '';
      $term_description = $term
        ->getDescription();
      if (!empty($term_description)) {
        $desc = '<div class="faq-qa-description">';
        $desc .= $term_description . "</div>";
      }
      $query = $this->database
        ->select('node', 'n');
      $query
        ->join('node_field_data', 'd', 'n.nid = d.nid');
      $query
        ->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
      $term_node_count = $query
        ->condition('d.status', 1)
        ->condition('n.type', 'faq')
        ->condition("ti.tid", $term_id)
        ->addTag('node_access')
        ->countQuery()
        ->execute()
        ->fetchField();
      if ($term_node_count > 0) {
        $path = Url::fromUserInput('/faq-page/' . $term_id);

        // Pathauto is not exists in D8 yet
        // if (!\Drupal::service('path.alias_manager.cached')->getPathAlias(arg(0) . '/' . $tid) && $this->moduleHandler()->moduleExists('pathauto')) {
        // }.
        if ($display_faq_count) {
          $count = $term_node_count;
          if ($hide_child_terms) {
            $count = $tree_count;
          }
          $cur_item = $this->linkGenerator
            ->generate($this
            ->t($term
            ->getName()), $path) . " ({$count}) " . $desc;
        }
        else {
          $cur_item = $this->linkGenerator
            ->generate($this
            ->t($term
            ->getName()), $path) . $desc;
        }
      }
      else {
        $cur_item = $this
          ->t($term
          ->getName()) . $desc;
      }
      if (!empty($term_image)) {
        $cur_item .= '<div class="clear-block"></div>';
      }
      $term_items = array();
      if (!$hide_child_terms) {
        $term_items = $this
          ->_getIndentedFaqTerms($vid, $term_id);
      }
      $items[] = array(
        "item" => $cur_item,
        "children" => $term_items,
      );
    }
  }
  return $items;
}