View source
<?php
namespace Drupal\tagclouds\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Vocabulary;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\tagclouds\TagService;
use Drupal\tagclouds\CloudBuilder;
use Drupal\Core\Cache\Cache;
class TagcloudsTermsBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $tagService;
protected $cloudBuilder;
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, TagService $tagService, CloudBuilder $cloudBuilder, ConfigFactoryInterface $configFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->tagService = $tagService;
$this->cloudBuilder = $cloudBuilder;
$this->configFactory = $configFactory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('tagclouds.tag'), $container
->get('tagclouds.cloud_builder'), $container
->get('config.factory'));
}
public function defaultConfiguration() {
return [
'cache' => [
'max_age' => 0,
'contexts' => [],
],
];
}
public function blockForm($form, FormStateInterface $form_state) {
$terms_limit = isset($this->configuration['tags']) ? $this->configuration['tags'] : 0;
$vocabulary = isset($this->configuration['vocabulary']) ? $this->configuration['vocabulary'] : 'tags';
$form['tags'] = [
'#type' => 'textfield',
'#title' => $this
->t('Tags to show'),
'#default_value' => $terms_limit,
'#maxlength' => 3,
'#description' => $this
->t("The number of tags to show in this block. Enter '0' to display all tags."),
];
$form['vocabulary'] = [
'#type' => 'textfield',
'#title' => $this
->t('Vocabulary machine name'),
'#default_value' => $vocabulary,
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['tags'] = $form_state
->getValue('tags');
$this->configuration['vocabulary'] = $form_state
->getValue('vocabulary');
}
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;
}
public function getCacheTags() {
return Cache::mergeTags(parent::getCacheTags(), [
'node_list',
'config:tagclouds.settings',
'taxonomy_term_list',
]);
}
}