You are here

public function Utility::faqAskUnansweredBuild in FAQ_Ask 8

Create a categorized list of nodes that are not answered.

1 call to Utility::faqAskUnansweredBuild()
template_preprocess_faq_ask_unanswered in ./faq_ask.module
Create a categorized list of nodes that are not answered.

File

src/Utility.php, line 263
Contains \Drupal\faq_ask\Utility.

Class

Utility
Contains static helper functions for FAQ module.

Namespace

Drupal\faq_ask

Code

public function faqAskUnansweredBuild(&$variables) {
  $data = $variables['data'];

  // Fetch the term from term id.
  $tid = $variables['term'];
  $term = taxonomy_term_load($tid);
  $class = $variables['class'];
  $mode = $variables['mode'];

  // Get number of questions, and account for hidden sub-categories.
  $count = count($data);

  // Module Handler.
  $moduleHandler = \Drupal::moduleHandler();

  // Get taxonomy image.
  $variables['term_image'] = '';
  if ($moduleHandler
    ->moduleExists('taxonomy_image')) {
    $variables['term_image'] = taxonomy_image_display($term
      ->id(), array(
      'class' => 'faq-tax-image',
    ));
  }

  // Configure header.
  if (is_object($term)) {
    $variables['category_depth'] = !empty($term->depth) ? $term->depth : 1;
    $variables['category_name'] = SafeMarkup::checkPlain($term
      ->getName());
    $variables['header_title'] = SafeMarkup::checkPlain($term
      ->getName());

    // Configure category description.
    $variables['description'] = SafeMarkup::checkPlain($term
      ->get('description')->value);
  }
  else {
    $variables['category_depth'] = 1;
    $variables['category_name'] = t('Uncategorized');
    $variables['header_title'] = t('Uncategorized');

    // Configure category description.
    $variables['description'] = t('Nodes that are not assigned to any category yet. This must be done when answering the question.');
  }

  // Configure class (faq-qa or faq-qa-hide).
  $variables['container_class'] = 'faq-qa';
  if (!count($data)) {
    $variables['question_count'] = $count;
    $variables['nodes'] = array();
    return;
  }
  $language = \Drupal::languageManager()
    ->getCurrentLanguage();
  $nodes = array();
  foreach ($data as $nid) {
    $node = node_load($nid);
    $node_var = array();
    $anchor = 't' . $tid . 'n' . $node
      ->id();
    FaqViewer::viewQuestion($node_var, $node, '1', $anchor);
    entity_view($node, 'teaser', $language);

    // Add "edit answer" link if they have the correct permissions.
    if (faq_ask_node_access($node, 'update')) {
      $node->content['links']['node'][] = array(
        'title' => 'Edit answer',
        'href' => Url::fromUserInput("/node/" . $node
          ->id() . "/edit", array(
          "query" => drupal_get_destination(),
        ))
          ->toString(),
      );
    }

    // Add "answer question" link if they have the correct permissions.
    if (\Drupal::currentUser()
      ->hasPermission('answer question')) {
      $token = Utility::faq_ask_get_token('faq_ask/answer/' . $nid);
      $node->content['links']['node'][] = array(
        'title' => 'Answer question',
        'href' => Url::fromUserInput('/faq_ask/' . $mode . '/' . $node
          ->id(), array(
          "query" => array(
            'token' => $token,
          ),
        ))
          ->toString(),
      );
    }
    $build = $node->content;

    // We don't need duplicate rendering info in node->content.
    unset($node->content);
    $build += array(
      '#theme' => 'node',
      '#node' => $node,
      '#view_mode' => 'teaser',
      '#language' => $language,
    );

    // Add contextual links for this node.
    $build['#contextual_links']['node'] = array(
      'node',
      array(
        $node
          ->id(),
      ),
    );
    $node_links = $build['links']['node'];
    unset($build['links']);

    // We don't want node title displayed.
    unset($build['#theme']);
    $node_var['body'] = drupal_render($build);
    $node_var['links'] = $node_links;
    $nodes[] = $node_var;
  }
  $variables['nodes'] = $nodes;
  $variables['question_count'] = $count;
}