You are here

public function FaqCategoriesBlock::build in Frequently Asked Questions 8

Implements \Drupal\block\BlockBase::blockBuild().

Overrides BlockPluginInterface::build

File

src/Plugin/Block/FaqCategoriesBlock.php, line 24

Class

FaqCategoriesBlock
Provides a simple block.

Namespace

Drupal\faq\Plugin\Block

Code

public function build() {
  static $vocabularies, $terms;
  $items = [];
  $faq_settings = \Drupal::configFactory()
    ->get('faq.settings');
  if (!$faq_settings
    ->get('use_categories')) {
    return [];
  }
  $moduleHandler = \Drupal::moduleHandler();
  if ($moduleHandler
    ->moduleExists('taxonomy')) {
    if (!isset($terms)) {
      $terms = [];
      $vocabularies = Vocabulary::loadMultiple();
      $vocab_omit = array_flip($faq_settings
        ->get('omit_vocabulary'));
      $vocabularies = array_diff_key($vocabularies, $vocab_omit);
      foreach ($vocabularies as $vocab) {
        foreach (\Drupal::entityTypeManager()
          ->getStorage('taxonomy_term')
          ->loadTree($vocab
          ->id()) as $term) {
          if (FaqHelper::taxonomyTermCountNodes($term->tid)) {
            $terms[$term->name] = $term->tid;
          }
        }
      }
    }
    if (count($terms) > 0) {
      foreach ($terms as $name => $tid) {
        $items[] = \Drupal::linkGenerator()
          ->generate($name, Url::fromUserInput('/faq-page/' . $tid));
      }
    }
  }
  return [
    '#theme' => 'item_list',
    '#items' => $items,
    '#list_type' => $faq_settings
      ->get('category_listing'),
  ];
}