You are here

public function HierarchicalTaxonomyMenuBlock::build in Hierarchical Taxonomy Menu 8

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/HierarchicalTaxonomyMenuBlock.php, line 568

Class

HierarchicalTaxonomyMenuBlock
Provides a 'HierarchicalTaxonomyMenuBlock' block.

Namespace

Drupal\hierarchical_taxonomy_menu\Plugin\Block

Code

public function build() {
  $vocabulary_config = $this->configuration['vocabulary'];
  $vocabulary_config = explode('|', $vocabulary_config);
  $vocabulary = isset($vocabulary_config[0]) ? $vocabulary_config[0] : NULL;
  $base_term = $this
    ->getVocabularyBaseTerm($this->configuration['base_term'], $this->configuration['dynamic_base_term']);
  $max_depth = $this->configuration['max_depth'];
  $vocabulary_tree = $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadTree($vocabulary, $base_term, $max_depth + 1);
  if ($this->configuration['hide_block'] && !$vocabulary_tree) {
    return [];
  }
  $image_field = isset($vocabulary_config[1]) ? $vocabulary_config[1] : NULL;
  $use_image_style = $this->configuration['use_image_style'];
  $image_height = $this->configuration['image_height'];
  $image_width = $this->configuration['image_width'];
  $image_style = $use_image_style == TRUE ? $this->configuration['image_style'] : NULL;
  $route_tid = $this
    ->getCurrentRoute();
  $max_age = $this
    ->getMaxAge($this->configuration['max_age']);
  $interactive_parent = $this->configuration['collapsible'] ? $this->configuration['interactive_parent'] : 0;
  $show_count = $this->configuration['show_count'];
  $referencing_field = $this->configuration['referencing_field'];
  $vocabulary_tree_array = [];
  foreach ($vocabulary_tree as $item) {
    $vocabulary_tree_array[] = [
      'tid' => $item->tid,
      'status' => $this
        ->getStatusFromTid($item->tid),
      'name' => $this
        ->getNameFromTid($item->tid),
      'url' => $this
        ->getLinkFromTid($item->tid),
      'parents' => $item->parents,
      'use_image_style' => $use_image_style,
      'image' => $this
        ->getImageFromTid($item->tid, $image_field, $image_style),
      'height' => $image_height != '' ? $image_height : 16,
      'width' => $image_width != '' ? $image_width : 16,
      'interactive_parent' => $interactive_parent,
      'show_count' => $show_count,
      'entities' => !empty($show_count) ? $this
        ->getEntityIds($this->entitiesMap[$show_count], $referencing_field, $item->tid, $vocabulary, $this->configuration['calculate_count_recursively']) : [],
    ];
  }
  $tree = $this
    ->generateTree($vocabulary_tree_array, $base_term);
  return [
    '#theme' => 'hierarchical_taxonomy_menu',
    '#menu_tree' => $tree,
    '#route_tid' => $route_tid,
    '#cache' => [
      'max-age' => $max_age,
      'tags' => [
        'taxonomy_term_list',
      ],
    ],
    '#current_depth' => 0,
    '#vocabulary' => $vocabulary,
    '#max_depth' => $max_depth,
    '#collapsible' => $this->configuration['collapsible'],
    '#attached' => [
      'library' => [
        'hierarchical_taxonomy_menu/hierarchical_taxonomy_menu',
      ],
      'drupalSettings' => [
        'stayOpen' => $this->configuration['stay_open'],
        'interactiveParentMenu' => $this->configuration['interactive_parent'],
      ],
    ],
  ];
}