You are here

public static function FaqHelper::getChildCategoriesFaqs in Frequently Asked Questions 8

Helper function for retrieving the sub-categories faqs.

Parameters

$term:

$theme_function:

$default_weight:

$default_sorting:

$category_display:

$class:

null $parent_term:

Return value

array

5 calls to FaqHelper::getChildCategoriesFaqs()
template_preprocess_faq_category_hide_answer in includes/faq.hide_answer.inc
Create categorized FAQ page if set to show answer when question is clicked.
template_preprocess_faq_category_new_page in includes/faq.new_page.inc
Create categorized FAQ page if set to show answer in a new page.
template_preprocess_faq_category_questions_inline in includes/faq.questions_inline.inc
Create categorized FAQ page if set to show/hide the questions inline.
template_preprocess_faq_category_questions_top in includes/faq.questions_top.inc
Create categorized questions for FAQ page if set to show questions on top.
template_preprocess_faq_category_questions_top_answers in includes/faq.questions_top.inc
Create categorized answers for FAQ page if set to show the questions on top.

File

src/FaqHelper.php, line 106

Class

FaqHelper
Contains static helper functions for FAQ module.

Namespace

Drupal\faq

Code

public static function getChildCategoriesFaqs($term, $theme_function, $default_weight, $default_sorting, $category_display, $class, $parent_term = NULL) {
  $output = array();
  $list = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadChildren($term
    ->id());
  if (!is_array($list)) {
    return '';
  }
  foreach ($list as $tid => $child_term) {
    $child_term->depth = $term->depth + 1;
    if (FaqHelper::taxonomyTermCountNodes($child_term
      ->id())) {
      $query = \Drupal::database()
        ->select('node', 'n');
      $query
        ->join('node_field_data', 'd', 'n.nid = d.nid');
      $ti_alias = $query
        ->innerJoin('taxonomy_index', 'ti', '(n.nid = %alias.nid)');
      $w_alias = $query
        ->leftJoin('faq_weights', 'w', "%alias.tid = {$ti_alias}.tid AND n.nid = %alias.nid");
      $query
        ->fields('n', array(
        'nid',
      ))
        ->condition('n.type', 'faq')
        ->condition('d.status', 1)
        ->condition("{$ti_alias}.tid", $child_term
        ->id())
        ->addTag('node_access');
      $default_weight = 0;
      if ($default_sorting == 'ASC') {
        $default_weight = 1000000;
      }

      // Works, but involves variable concatenation - safe though, since
      // $default_weight is an integer.
      $query
        ->addExpression("COALESCE(w.weight, {$default_weight})", 'effective_weight');

      // Doesn't work in Postgres.
      // $query->addExpression('COALESCE(w.weight, CAST(:default_weight as SIGNED))', 'effective_weight', array(':default_weight' => $default_weight));.
      $query
        ->orderBy('effective_weight', 'ASC')
        ->orderBy('d.sticky', 'DESC');
      if ($default_sorting == 'ASC') {
        $query
          ->orderBy('d.created', 'ASC');
      }
      else {
        $query
          ->orderBy('d.created', 'DESC');
      }

      // We only want the first column, which is nid, so that we can load all
      // related nodes.
      $nids = $query
        ->execute()
        ->fetchCol();
      $data = Node::loadMultiple($nids);
      $to_render = array(
        '#theme' => $theme_function,
        '#data' => $data,
        '#display_header' => 1,
        '#category_display' => $category_display,
        '#term' => $child_term,
        '#class' => $class,
        '#parent_term' => $parent_term,
      );
      $output[] = \Drupal::service('renderer')
        ->render($to_render);
    }
  }
  return $output;
}