You are here

public static function FaqViewer::viewQuestion in Frequently Asked Questions 8

Helper function to setup the faq question.

Parameters

array &$data: Array reference to store display data in.

\Drupal\node\NodeInterface $node: The node object.

string $path: The path/url which the question should link to if links are disabled.

string $anchor: Link anchor to use in question links.

7 calls to FaqViewer::viewQuestion()
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_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.
template_preprocess_faq_hide_answer in includes/faq.hide_answer.inc
Create FAQ page if set to show/hide answer when question is clicked.

... See full list

File

src/FaqViewer.php, line 27

Class

FaqViewer
Controlls the display of questions and answers.

Namespace

Drupal\faq

Code

public static function viewQuestion(&$data, NodeInterface $node, $path = NULL, $anchor = NULL) {
  $faq_settings = \Drupal::config('faq.settings');
  $disable_node_links = $faq_settings
    ->get('disable_node_links');
  $question = '';

  // Don't link to faq node, instead provide no link, or link to current page.
  if ($disable_node_links) {
    if (empty($path) && empty($anchor)) {
      $question = $node
        ->getTitle();
    }
    elseif (empty($path)) {

      // Can't seem to use l() function with empty string as screen-readers
      // don't like it, so create anchor name manually.
      $question = '<a id="' . $anchor . '"></a>' . $node
        ->getTitle();
    }
    else {
      $options = array();
      if ($anchor) {
        $options['attributes'] = array(
          'id' => $anchor,
        );
      }
      $question = Link::fromTextAndUrl($node
        ->getTitle(), $path, $options)
        ->toString();
    }
  }
  else {
    $node_id = $node
      ->id();
    if (empty($anchor)) {
      $question = Link::fromTextAndUrl($node
        ->getTitle(), "node/{$node_id})")
        ->toString();
    }
    else {
      $url = $node
        ->toUrl()
        ->setOptions(array(
        "attributes" => array(
          "id" => "{$anchor}",
        ),
      ));
      $question = Link::fromTextAndUrl($node
        ->getTitle(), $url)
        ->toString();
    }
  }
  $question = '<span datatype="" property="dc:title">' . $question . '</span>';
  $detailed_question = $node
    ->get('field_detailed_question')->value;
  if ($faq_settings
    ->get('display') != 'hide_answer' && !empty($detailed_question) && $faq_settings
    ->get('question_length') == 'both') {
    $question .= '<div class="faq-detailed-question">' . $detailed_question . '</div>';
  }
  $data['question'] = new FormattableMarkup($question, []);
}