Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupalgcl_tag_cloudPluginBlock;
- use DrupalComponentUtilityUrlHelper;
- use DrupalCoreBlockBlockBase;
- use DrupalCoreFormFormStateInterface;
- use DrupalCorePluginContainerFactoryPluginInterface;
- use DrupalCoreUrl;
- use DrupaltaxonomyEntityVocabulary;
- use DrupaltaxonomyTermStorageInterface;
- use SymfonyComponentDependencyInjectionContainerInterface;
- /**
- * Provides a 'TagCloudBlock' block.
- *
- * @Block(
- * id = "gcl_tag_cloud_block",
- * admin_label = @Translation("Tag cloud block"),
- * )
- */
- class TagCloudBlock extends BlockBase implements ContainerFactoryPluginInterface {
- protected $term_storage;
- protected $token_service;
- public function __construct(array $configuration, $plugin_id, $plugin_definition, TermStorageInterface $term_storage, $token_service) {
- parent::__construct($configuration, $plugin_id, $plugin_definition);
- $this->term_storage = $term_storage;
- $this->token_service = $token_service;
- }
- public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition){
- return new static(
- $configuration,
- $plugin_id,
- $plugin_definition,
- $container->get('entity.manager')->getStorage("taxonomy_term"),
- $container->get('token')
- );
- }
- /**
- * {@inheritdoc}
- */
- public function blockForm($form, FormStateInterface $form_state) {
- $form = parent::blockForm($form, $form_state);
- // Get the configurations.
- $config = $this->getConfiguration();
- $vocabularies = Vocabulary::loadMultiple();
- $vocabulary_options = [];
- foreach ($vocabularies as $vocabulary) {
- $vid = $vocabulary->get('vid');
- $name = $vocabulary->get('name');
- $vocabulary_options[$vid] = $name;
- }
- $form['vocabularies'] = [
- '#type' => 'checkboxes',
- '#options' => $vocabulary_options,
- '#title' => t('Vocabularies'),
- '#required' => TRUE,
- '#default_value' => $config['vocabularies'],
- ];
- $form['redirect_url'] = [
- '#type' => 'textfield',
- '#title' => t('Redirect url'),
- '#required' => FALSE,
- '#default_value' => isset($config['redirect_url']) ? $config['redirect_url'] : 'tags/',
- ];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function blockValidate($form, FormStateInterface $form_state) {
- $redirect_url = $form_state->getValue('redirect_url');
- if (UrlHelper::isExternal($redirect_url)) {
- $form_state->setErrorByName('redirect_url', t('External link is not allowed for redirect url.'));
- }
- }
- /**
- * {@inheritdoc}
- */
- public function blockSubmit($form, FormStateInterface $form_state) {
- $this->setConfigurationValue('vocabularies', $form_state->getValue('vocabularies'));
- $this->setConfigurationValue('redirect_url', $form_state->getValue('redirect_url'));
- }
- /**
- * {@inheritdoc}
- */
- public function build() {
- $config = $this->getConfiguration();
- $vocabularies_selected = $config['vocabularies'];
- $terms = [];
- foreach ($vocabularies_selected as $vid) {
- $vocabulary_terms = $this->term_storage->loadTree($vid);
- foreach ($vocabulary_terms as $term) {
- $term = $this->term_storage->load($term->tid);
- $url = $this->token_service->replace(
- $config['redirect_url'],
- ['term' => $term]
- );
- $terms[$term->id()] = [
- 'name' => $term->getName(),
- 'url' => $url . $term->id(),
- ];
- }
- }
- $build = [];
- $build['gcl_tag_cloud_block'] = [
- '#theme' => 'gcl_tag_cloud_block',
- '#tags' => $terms,
- ];
- return $build;
- }
- }
- <h2>Tag Cloud</h2>
- {% for term_id, term in tags %}
- <a href="{{ term.url }}">{{ term.name }}</a>
- <br>
- {% endfor %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement