You are here

public function CatalogBlock::build in Ubercart 8.4

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

uc_catalog/src/Plugin/Block/CatalogBlock.php, line 135

Class

CatalogBlock
Provides the product catalog block.

Namespace

Drupal\uc_catalog\Plugin\Block

Code

public function build() {

  // Get the vocabulary tree information.
  $vid = $this->configFactory
    ->get('uc_catalog.settings')
    ->get('vocabulary');
  $tree = $this->entityTypeManager
    ->getStorage('taxonomy_term')
    ->loadTree($vid);

  // Then convert it into an actual tree structure.
  $seq = 0;
  $menu_tree = new TreeNode();
  foreach ($tree as $knot) {
    $seq++;
    $knot->sequence = $seq;
    $knothole = new TreeNode($knot);

    // Begin at the root of the tree and find the proper place.
    $menu_tree
      ->addChild($knothole);
  }

  // @todo Theme function should use block configuration, passed here,
  // not uc_catalog.settings taken from \Drupal::config().
  $build['content'] = [
    '#theme' => 'uc_catalog_block',
    '#menu_tree' => $menu_tree,
  ];
  $build['#attached']['library'][] = 'uc_catalog/uc_catalog.styles';
  return $build;
}