SHARE
TWEET

Untitled

a guest Sep 13th, 2017 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Drupalgcl_tag_cloudPluginBlock;
  4.  
  5. use DrupalComponentUtilityUrlHelper;
  6. use DrupalCoreBlockBlockBase;
  7. use DrupalCoreFormFormStateInterface;
  8. use DrupalCorePluginContainerFactoryPluginInterface;
  9. use DrupalCoreUrl;
  10. use DrupaltaxonomyEntityVocabulary;
  11. use DrupaltaxonomyTermStorageInterface;
  12. use SymfonyComponentDependencyInjectionContainerInterface;
  13.  
  14. /**
  15.  * Provides a 'TagCloudBlock' block.
  16.  *
  17.  * @Block(
  18.  *  id = "gcl_tag_cloud_block",
  19.  *  admin_label = @Translation("Tag cloud block"),
  20.  * )
  21.  */
  22. class TagCloudBlock extends BlockBase implements ContainerFactoryPluginInterface {
  23.  
  24.   protected $term_storage;
  25.   protected $token_service;
  26.  
  27.   public function __construct(array $configuration, $plugin_id, $plugin_definition, TermStorageInterface $term_storage, $token_service) {
  28.     parent::__construct($configuration, $plugin_id, $plugin_definition);
  29.     $this->term_storage = $term_storage;
  30.     $this->token_service = $token_service;
  31.   }
  32.  
  33.   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition){
  34. return new static(
  35.   $configuration,
  36.   $plugin_id,
  37.   $plugin_definition,
  38.   $container->get('entity.manager')->getStorage("taxonomy_term"),
  39.   $container->get('token')
  40. );
  41.   }
  42.  
  43.   /**
  44.    * {@inheritdoc}
  45.    */
  46.   public function blockForm($form, FormStateInterface $form_state) {
  47.     $form = parent::blockForm($form, $form_state);
  48.  
  49.     // Get the configurations.
  50.     $config = $this->getConfiguration();
  51.  
  52.     $vocabularies = Vocabulary::loadMultiple();
  53.     $vocabulary_options = [];
  54.     foreach ($vocabularies as $vocabulary) {
  55.       $vid = $vocabulary->get('vid');
  56.       $name = $vocabulary->get('name');
  57.       $vocabulary_options[$vid] = $name;
  58.     }
  59.  
  60.     $form['vocabularies'] = [
  61.       '#type' => 'checkboxes',
  62.       '#options' => $vocabulary_options,
  63.       '#title' => t('Vocabularies'),
  64.       '#required' => TRUE,
  65.       '#default_value' => $config['vocabularies'],
  66.     ];
  67.  
  68.     $form['redirect_url'] = [
  69.       '#type' => 'textfield',
  70.       '#title' => t('Redirect url'),
  71.       '#required' => FALSE,
  72.       '#default_value' => isset($config['redirect_url']) ? $config['redirect_url'] : 'tags/',
  73.     ];
  74.     return $form;
  75.   }
  76.  
  77.   /**
  78.    * {@inheritdoc}
  79.    */
  80.   public function blockValidate($form, FormStateInterface $form_state) {
  81.     $redirect_url = $form_state->getValue('redirect_url');
  82.     if (UrlHelper::isExternal($redirect_url)) {
  83.       $form_state->setErrorByName('redirect_url', t('External link is not allowed for redirect url.'));
  84.     }
  85.   }
  86.  
  87.   /**
  88.    * {@inheritdoc}
  89.    */
  90.   public function blockSubmit($form, FormStateInterface $form_state) {
  91.     $this->setConfigurationValue('vocabularies', $form_state->getValue('vocabularies'));
  92.     $this->setConfigurationValue('redirect_url', $form_state->getValue('redirect_url'));
  93.   }
  94.  
  95.  
  96.   /**
  97.    * {@inheritdoc}
  98.    */
  99.   public function build() {
  100.     $config = $this->getConfiguration();
  101.  
  102.     $vocabularies_selected = $config['vocabularies'];
  103.     $terms = [];
  104.     foreach ($vocabularies_selected as $vid) {
  105.       $vocabulary_terms = $this->term_storage->loadTree($vid);
  106.       foreach ($vocabulary_terms as $term) {
  107.         $term = $this->term_storage->load($term->tid);
  108.         $url = $this->token_service->replace(
  109.           $config['redirect_url'],
  110.           ['term' => $term]
  111.         );
  112.         $terms[$term->id()] = [
  113.           'name' => $term->getName(),
  114.           'url' => $url . $term->id(),
  115.         ];
  116.       }
  117.     }
  118.  
  119.     $build = [];
  120.     $build['gcl_tag_cloud_block'] = [
  121.       '#theme' => 'gcl_tag_cloud_block',
  122.       '#tags' => $terms,
  123.     ];
  124.  
  125.     return $build;
  126.   }
  127. }
  128.    
  129. <h2>Tag Cloud</h2>
  130. {% for term_id, term in tags %}
  131.     <a href="{{ term.url }}">{{ term.name }}</a>
  132.     <br>
  133. {% endfor %}
RAW Paste Data
Top