You are here

public function TagcloudsTermsBlock::build in TagCloud 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Block/TagcloudsTermsBlock.php \Drupal\tagclouds\Plugin\Block\TagcloudsTermsBlock::build()
  2. 1.0.x src/Plugin/Block/TagcloudsTermsBlock.php \Drupal\tagclouds\Plugin\Block\TagcloudsTermsBlock::build()

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/TagcloudsTermsBlock.php, line 133

Class

TagcloudsTermsBlock
Provides a template for blocks based of each vocabulary.

Namespace

Drupal\tagclouds\Plugin\Block

Code

public function build() {
  $tags_limit = $this->configuration['tags'];
  $vocab_name = $this->configuration['vocabulary'];
  $content = [
    '#attached' => [
      'library' => 'tagclouds/clouds',
    ],
  ];
  if ($voc = Vocabulary::load($vocab_name)) {
    $tags = $this->tagService
      ->getTags([
      $vocab_name,
    ], $this->configFactory
      ->getEditable('tagclouds.settings')
      ->get('levels'), $tags_limit);
    $tags = $this->tagService
      ->sortTags($tags);
    $content[] = [
      'tags' => $this->cloudBuilder
        ->build($tags),
    ];
    if (count($tags) >= $tags_limit && $tags_limit > 0) {
      $content[] = [
        '#type' => 'more_link',
        '#title' => $this
          ->t('more tags'),
        '#url' => Url::fromRoute('tagclouds.chunk_vocs', [
          'tagclouds_vocs_str' => $voc
            ->id(),
        ]),
      ];
    }
  }
  return $content;
}