You are here

private function FaqController::_displayFaqByCategory in Frequently Asked Questions 8

Display FAQ questions and answers filtered by category.

Parameters

$faq_display Define the way the FAQ is being shown; can have the values:: 'questions top',hide answers','questions inline','new page'.

$category_display The layout of categories which should be used.:

$term The category / term to display FAQs for.:

$display_header Set if the header will be shown or not.:

&$output Reference which holds the content of the page, HTML formatted.:

&$output_answer Reference which holds the answers from the FAQ, when showing questions on top.:

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

File

src/Controller/FaqController.php, line 361

Class

FaqController
Controller routines for FAQ routes.

Namespace

Drupal\faq\Controller

Code

private function _displayFaqByCategory($faq_display, $category_display, $term, $display_header, &$output, &$output_answers) {
  $langcode = $this->languageManager
    ->getCurrentLanguage()
    ->getId();
  $default_sorting = $this->config
    ->get('faq.settings')
    ->get('default_sorting');
  $term_id = $term
    ->id();
  $query = $this->database
    ->select('node', 'n');
  $query
    ->join('node_field_data', 'd', 'd.nid = n.nid');
  $query
    ->innerJoin('taxonomy_index', 'ti', 'n.nid = ti.nid');
  $query
    ->leftJoin('faq_weights', 'w', 'w.tid = ti.tid AND n.nid = w.nid');
  $query
    ->fields('n', [
    'nid',
  ])
    ->condition('n.type', 'faq')
    ->condition('d.langcode', $langcode)
    ->condition('d.status', 1)
    ->condition("ti.tid", $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);
  foreach ($data as $key => &$node) {
    $node = $node
      ->hasTranslation($langcode) ? $node
      ->getTranslation($langcode) : $node;
  }

  // Handle indenting of categories.
  $depth = 0;
  if (!isset($term->depth)) {
    $children = $this->entityTypeManager
      ->getStorage('taxonomy_term')
      ->loadChildren($term
      ->id());
    $term->depth = count($children);
  }
  while ($depth < $term->depth) {
    $display_header = 1;
    $indent = '<div class="faq-category-indent">';
    $output .= $indent;
    $depth++;
  }

  // Set up the class name for hiding the q/a for a category if required.
  $faq_class = "faq-qa";
  if ($category_display == "hide_qa") {
    $faq_class = "faq-qa-hide";
  }
  $output_render = $output_answers_render = array(
    '#data' => $data,
    '#display_header' => $display_header,
    '#category_display' => $category_display,
    '#term' => $term,
    '#class' => $faq_class,
    '#parent_term' => $term,
  );
  switch ($faq_display) {
    case 'questions_top':
      $output_render['#theme'] = 'faq_category_questions_top';
      $output .= $this->renderer
        ->render($output_render);
      $output_answers_render['#theme'] = 'faq_category_questions_top_answers';
      $output_answers .= $this->renderer
        ->render($output_answers_render);
      break;
    case 'hide_answer':
      $output_render['#theme'] = 'faq_category_hide_answer';
      $output .= $this->renderer
        ->render($output_render);
      break;
    case 'questions_inline':
      $output_render['#theme'] = 'faq_category_questions_inline';
      $output .= $this->renderer
        ->render($output_render);
      break;
    case 'new_page':
      $output_render['#theme'] = 'faq_category_new_page';
      $output .= $this->renderer
        ->render($output_render);
      break;
  }

  // Handle indenting of categories.
  while ($depth > 0) {
    $output .= '</div>';
    $depth--;
  }
}