diff --git a/src/Plugin/Block/VocabularyCatalogBlock.php b/src/Plugin/Block/VocabularyCatalogBlock.php index f42a0b3..2562c38 100644 --- a/src/Plugin/Block/VocabularyCatalogBlock.php +++ b/src/Plugin/Block/VocabularyCatalogBlock.php @@ -4,8 +4,8 @@ namespace Drupal\vocabulary_catalog\Plugin\Block; use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Url; -Use Drupal\taxonomy\Entity\Vocabulary; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a Block to display last visited pages you visited on the website. @@ -17,152 +17,190 @@ Use Drupal\taxonomy\Entity\Vocabulary; */ class VocabularyCatalogBlock extends BlockBase { - /** - * {@inheritdoc} - */ - public function defaultConfiguration() { - $this->configuration['vocabularies'] = []; - $this->configuration['disabled_terms'] = false; - $this->configuration['vocabulary_listing_style'] = '1'; - $this->configuration['terms_display_layout'] = '1'; - $this->configuration['wrapper_class'] = ''; - return ['label_display' => FALSE]; - } + /** + * The entity type manager. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * Constructs a VocabularyCatalogBlock object. + * + * @param array $configuration + * The block configuration. + * @param string $plugin_id + * The plugin ID for the block. + * @param mixed $plugin_definition + * The plugin definition. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->entityTypeManager = $entity_type_manager; + } - /** - * {@inheritdoc} - */ - public function build() { - $content = array(); - $vocabularies = $this->configuration['vocabularies']; - $active_vocabularies = array_keys(array_filter($vocabularies)); - - $disabled_terms = $this->configuration['disabled_terms']; - $vocabulary_listing_style = $this->configuration['vocabulary_listing_style']; - $terms_display_layout = $this->configuration['terms_display_layout']; - $wrapper_class = $this->configuration['wrapper_class']; - $label_display = $this->configuration['label_display']; - $block_label = ''; - - if($label_display) { - $block_label = $this->t($this->label()); - } + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager') + ); + } - $config_data = array( - 'active_vocabularies' => $active_vocabularies, - 'disabled_terms' => $disabled_terms, - 'terms_display_layout' => $terms_display_layout - ); + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + $this->configuration['vocabularies'] = []; + $this->configuration['disabled_terms'] = FALSE; + $this->configuration['vocabulary_listing_style'] = '1'; + $this->configuration['terms_display_layout'] = '1'; + $this->configuration['wrapper_class'] = ''; + return ['label_display' => FALSE]; + } - $catalog_data = getVocabularyCatalog($config_data); + /** + * {@inheritdoc} + */ + public function build() { + $content = []; + $vocabularies = $this->configuration['vocabularies']; + $active_vocabularies = array_keys(array_filter($vocabularies)); + + $disabled_terms = $this->configuration['disabled_terms']; + $vocabulary_listing_style = $this->configuration['vocabulary_listing_style']; + $terms_display_layout = $this->configuration['terms_display_layout']; + $wrapper_class = $this->configuration['wrapper_class']; + $label_display = $this->configuration['label_display']; + $block_label = ''; + + if ($label_display) { + $label = $this->label(); + $block_label = $this->t('@label', ['@label' => $label]); + } - $content['vocabulary_listing_style'] = $vocabulary_listing_style; - $content['catalog_data'] = $catalog_data; - $content['wrapper_class'] = $wrapper_class; - $content['wrapper_class'] = $wrapper_class; - $content['block_label'] = $block_label; + $config_data = [ + 'active_vocabularies' => $active_vocabularies, + 'disabled_terms' => $disabled_terms, + 'terms_display_layout' => $terms_display_layout, + ]; - $build = []; - $build['vocabulary_catalog'] = [ - '#theme' => 'vocabulary_catalog_block', - '#content' => $content, - ]; + $catalog_data = vocabulary_catalog_get_vocabulary_catalog($config_data); - $build['#attached']['library'][] = 'vocabulary_catalog/vocabulary_catalog.frontend'; - return $build; - } + $content['vocabulary_listing_style'] = $vocabulary_listing_style; + $content['catalog_data'] = $catalog_data; + $content['wrapper_class'] = $wrapper_class; + $content['wrapper_class'] = $wrapper_class; + $content['block_label'] = $block_label; - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $form = parent::buildConfigurationForm($form, $form_state); - $config = $this->getConfiguration(); - - // Get all vocabularies - $vocabularies = Vocabulary::loadMultiple(); - $vocabulary_options = array(); - if($vocabularies) { - foreach($vocabularies as $voc) { - $vocabulary_options[$voc->id()] = $this->t($voc->label()); - } - } + $build = []; + $build['vocabulary_catalog'] = [ + '#theme' => 'vocabulary_catalog_block', + '#content' => $content, + ]; - $form['vocabularies'] = [ - '#type' => 'checkboxes', - '#title' => $this->t('Active Vocabularies'), - '#description' => $this->t('Select the available vocabulariess catalog.'), - '#default_value' => $config['vocabularies']??[], - '#options' => $vocabulary_options, - '#required' => true, - '#attributes' => ['class' => ['vocabularies']], - ]; - - $form['disabled_terms'] = [ - '#type' => 'checkbox', - '#title' => $this->t('Show disabled terms'), - '#description' => $this->t('Show disabled terms on the catalog. This options is useful for admin login.'), - '#default_value' => $config['disabled_terms']??false, - '#attributes' => ['class' => ['disabled-terms']], - ]; - - $form['vocabulary_listing_style'] = [ - '#type' => 'select', - '#title' => $this->t('Vocabulary listing style'), - '#description' => $this->t('Select the vocabulary listing style on the catalog.'), - '#default_value' => $config['vocabulary_listing_style']??'1', - '#options' => array( - '' => $this->t('None'), - '1' => $this->t('1) '), - '2' => $this->t('1. '), - '3' => $this->t('1 ') - ), - '#attributes' => ['class' => ['vocabulary-listing-style']], - ]; - - $form['terms_display_layout'] = [ - '#type' => 'radios', - '#title' => $this->t('Terms display layout'), - '#description' => $this->t('Select the terms display layout on the catalog.'), - '#default_value' => $config['terms_display_layout']??'1', - '#options' => array( - '1' => $this->t('Parent child tree structure'), - '2' => $this->t('Single child layout under vocabulary') - ), - '#required' => true, - '#attributes' => ['class' => ['terms-display-layout']], - ]; - - $form['wrapper_class'] = [ - '#type' => 'textfield', - '#title' => $this->t('Wrapper class'), - '#description' => $this->t('Custom class for the listing wrapper'), - '#default_value' => $config['wrapper_class']??'', - '#attributes' => ['class' => ['listing-calss']], - ]; - - return $form; - } + $build['#attached']['library'][] = 'vocabulary_catalog/vocabulary_catalog.frontend'; + return $build; + } - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - $this->configuration['vocabularies'] = $form_state->getValue('vocabularies'); - $this->configuration['disabled_terms'] = $form_state->getValue('disabled_terms'); - $this->configuration['vocabulary_listing_style'] = $form_state->getValue('vocabulary_listing_style'); - $this->configuration['terms_display_layout'] = $form_state->getValue('terms_display_layout'); - $this->configuration['wrapper_class'] = $form_state->getValue('wrapper_class'); - $this->configuration['label_display'] = $form_state->getValue('label_display'); + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + $config = $this->getConfiguration(); + + // Get all vocabularies. + $vocabularies = $this->entityTypeManager->getStorage('taxonomy_vocabulary')->loadMultiple(); + $vocabulary_options = []; + if ($vocabularies) { + foreach ($vocabularies as $voc) { + $voclabel = $voc->label(); + $vocabulary_options[$voc->id()] = $this->t('@voclabel', ['@voclabel' => $voclabel]); + } } - /** - * {@inheritdoc} - * - * disable block cache to keep it the Vocabulary Block update. - */ - public function getCacheMaxAge() - { - return 0; - } + $form['vocabularies'] = [ + '#type' => 'checkboxes', + '#title' => $this->t('Active Vocabularies'), + '#description' => $this->t('Select the available vocabulariess catalog.'), + '#default_value' => $config['vocabularies'] ?? [], + '#options' => $vocabulary_options, + '#required' => TRUE, + '#attributes' => ['class' => ['vocabularies']], + ]; + + $form['disabled_terms'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show disabled terms'), + '#description' => $this->t('Show disabled terms on the catalog. This options is useful for admin login.'), + '#default_value' => $config['disabled_terms'] ?? FALSE, + '#attributes' => ['class' => ['disabled-terms']], + ]; + + $form['vocabulary_listing_style'] = [ + '#type' => 'select', + '#title' => $this->t('Vocabulary listing style'), + '#description' => $this->t('Select the vocabulary listing style on the catalog.'), + '#default_value' => $config['vocabulary_listing_style'] ?? '1', + '#options' => [ + '' => $this->t('None'), + '1' => $this->t('1)'), + '2' => $this->t('1.'), + '3' => $this->t('1'), + ], + '#attributes' => ['class' => ['vocabulary-listing-style']], + ]; + + $form['terms_display_layout'] = [ + '#type' => 'radios', + '#title' => $this->t('Terms display layout'), + '#description' => $this->t('Select the terms display layout on the catalog.'), + '#default_value' => $config['terms_display_layout'] ?? '1', + '#options' => [ + '1' => $this->t('Parent child tree structure'), + '2' => $this->t('Single child layout under vocabulary'), + ], + '#required' => TRUE, + '#attributes' => ['class' => ['terms-display-layout']], + ]; + + $form['wrapper_class'] = [ + '#type' => 'textfield', + '#title' => $this->t('Wrapper class'), + '#description' => $this->t('Custom class for the listing wrapper'), + '#default_value' => $config['wrapper_class'] ?? '', + '#attributes' => ['class' => ['listing-calss']], + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['vocabularies'] = $form_state->getValue('vocabularies'); + $this->configuration['disabled_terms'] = $form_state->getValue('disabled_terms'); + $this->configuration['vocabulary_listing_style'] = $form_state->getValue('vocabulary_listing_style'); + $this->configuration['terms_display_layout'] = $form_state->getValue('terms_display_layout'); + $this->configuration['wrapper_class'] = $form_state->getValue('wrapper_class'); + $this->configuration['label_display'] = $form_state->getValue('label_display'); + } + + /** + * {@inheritdoc} + * + * Disable block cache to keep it the Vocabulary Block update. + */ + public function getCacheMaxAge() { + return 0; } + +} diff --git a/templates/vocabulary-catalog-block.html.twig b/templates/vocabulary-catalog-block.html.twig index d332b9c..ba04aa2 100755 --- a/templates/vocabulary-catalog-block.html.twig +++ b/templates/vocabulary-catalog-block.html.twig @@ -54,4 +54,4 @@ {% endfor %} {% endif %} - \ No newline at end of file + diff --git a/vocabulary_catalog.module b/vocabulary_catalog.module index 8bba752..c317098 100644 --- a/vocabulary_catalog.module +++ b/vocabulary_catalog.module @@ -1,8 +1,12 @@ ' . t('About') . ''; - $output .= '
' . t('Display the list of terms and sub terms with node count values.') . '
'; - return $output; -} + $output = '' . t('Display the list of terms and sub terms with node count values.') . '
'; + return $output; + } } /** - * Get the vocabulary catalog data - * @param [type] $config_data - * @return [type] + * Retrieves the catalog data for the active vocabularies. + * + * @param array $config_data + * The configuration data for the module. + * + * @return array + * The vocabulary catalog data. */ -function getVocabularyCatalog($config_data) { - $active_vocabularies = $config_data['active_vocabularies']; - $disabled_terms = $config_data['disabled_terms']; - $terms_display_layout = $config_data['terms_display_layout']; - - $vocabularies = Vocabulary::loadMultiple(); - $catalog_data = array(); - if($vocabularies) { - foreach($vocabularies as $voc) { - $voc_id = $voc->id(); - if(in_array($voc_id, $active_vocabularies)) { - $voc_label = $voc->label(); - $term_data = array(); - $active_tids = array(); - - $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $voc_id]); - $link = Link::fromTextAndUrl($voc_label, $url); - $link = $link->toRenderable(); - $voc_rendered = \Drupal::service('renderer')->render($link); - - $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($voc_id); - if($terms) { - foreach ($terms as $term) { - $status = $term->status; - $show_term = true; - - // if the terms is not published and if show only published terms is configured - // then hide the terms from catalog - if(!$disabled_terms && !$status) { - $show_term = false; - } - - if($show_term) { - $status = $term->status; - $tid = $term->tid; - $name = $term->name; - $parents = $term->parents; - $active_tids[] = $tid; - - $parent_ids = array_values($parents); - $parent_ids = array_filter($parent_ids); - $parent_id = (isset($parent_ids[0]))?$parent_ids[0]:0; - - $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]); - $link = Link::fromTextAndUrl($name, $url); - $link = $link->toRenderable(); - $rendered = \Drupal::service('renderer')->render($link); - - $query = \Drupal::database()->select('node_field_data', 'n'); - $query->leftJoin('taxonomy_index', 'ti', 'n.nid = ti.nid'); - $query->addField('n', 'nid'); - $query->fields('n', ['nid', 'type']); - $query->fields('ti', ['tid']); - $query->condition('ti.tid', $term->tid); - - $results = $query->execute(); - $node_query_results = $results->fetchAll(); - $count = count($node_query_results); - - $catalog_data[$voc_id]['terms_data'][] = array( - 'id' => $tid, - 'name' => $name, - 'count' => $count, - 'rendered' => $rendered, - 'parent_id' => $parent_id, - - ); - } - } +function vocabulary_catalog_get_vocabulary_catalog(array $config_data) { + $active_vocabularies = $config_data['active_vocabularies']; + $disabled_terms = $config_data['disabled_terms']; + $terms_display_layout = $config_data['terms_display_layout']; + + $vocabularies = Vocabulary::loadMultiple(); + $catalog_data = []; + if ($vocabularies) { + foreach ($vocabularies as $voc) { + $voc_id = $voc->id(); + if (in_array($voc_id, $active_vocabularies)) { + $voc_label = $voc->label(); + // $term_data = []; + $active_tids = []; + + $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $voc_id]); + $link = Link::fromTextAndUrl($voc_label, $url); + $link = $link->toRenderable(); + $voc_rendered = \Drupal::service('renderer')->render($link); + + $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($voc_id); + if ($terms) { + foreach ($terms as $term) { + $status = $term->status; + $show_term = TRUE; + + // If the terms is not published and if show only published terms is + // configured then hide the terms from catalog. + if (!$disabled_terms && !$status) { + $show_term = FALSE; } - if(!empty($active_tids)) { - $query = \Drupal::database()->select('node_field_data', 'n'); - $query->leftJoin('taxonomy_index', 'ti', 'n.nid = ti.nid'); - $query->addField('n', 'nid'); - $query->fields('n', ['nid', 'type']); - $query->fields('ti', ['tid']); - $query->condition('ti.tid', $active_tids, 'IN'); + if ($show_term) { + $status = $term->status; + $tid = $term->tid; + $name = $term->name; + $parents = $term->parents; + $active_tids[] = $tid; + + $parent_ids = array_values($parents); + $parent_ids = array_filter($parent_ids); + $parent_id = (isset($parent_ids[0])) ? $parent_ids[0] : 0; + + $url = Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $tid]); + $link = Link::fromTextAndUrl($name, $url); + $link = $link->toRenderable(); + $rendered = \Drupal::service('renderer')->render($link); + + $query = \Drupal::database()->select('node_field_data', 'n'); + $query->leftJoin('taxonomy_index', 'ti', 'n.nid = ti.nid'); + $query->addField('n', 'nid'); + $query->fields('n', ['nid', 'type']); + $query->fields('ti', ['tid']); + $query->condition('ti.tid', $term->tid); + + $results = $query->execute(); + $node_query_results = $results->fetchAll(); + $count = count($node_query_results); + + $catalog_data[$voc_id]['terms_data'][] = [ + 'id' => $tid, + 'name' => $name, + 'count' => $count, + 'rendered' => $rendered, + 'parent_id' => $parent_id, + + ]; + } + } + } - $results = $query->execute(); - $node_query_results = $results->fetchAll(); - $term_count = count($node_query_results); + if (!empty($active_tids)) { + $query = \Drupal::database()->select('node_field_data', 'n'); + $query->leftJoin('taxonomy_index', 'ti', 'n.nid = ti.nid'); + $query->addField('n', 'nid'); + $query->fields('n', ['nid', 'type']); + $query->fields('ti', ['tid']); + $query->condition('ti.tid', $active_tids, 'IN'); - $catalog_data[$voc_id]['count'] = $term_count; - $catalog_data[$voc_id]['rendered'] = $voc_rendered; - } + $results = $query->execute(); + $node_query_results = $results->fetchAll(); + $term_count = count($node_query_results); + $catalog_data[$voc_id]['count'] = $term_count; + $catalog_data[$voc_id]['rendered'] = $voc_rendered; } - // Display terms in tree structure - if($terms_display_layout == '1' && !empty($catalog_data)) { - foreach ($catalog_data as $key => $value) { - $tree = build_catalog_tree($value['terms_data'], 'parent_id', 'id'); - $catalog_data[$key]['terms_data'] = $tree; - } + } + + // Display terms in tree structure. + if ($terms_display_layout == '1' && !empty($catalog_data)) { + foreach ($catalog_data as $key => $value) { + $tree = vocabulary_catalog_build_catalog_tree($value['terms_data'], 'parent_id', 'id'); + $catalog_data[$key]['terms_data'] = $tree; } + } } -} + } -return $catalog_data; + return $catalog_data; } /** - * @param array $flatList - a flat list of tree nodes; a node is an array with keys: id, parent_id, name. + * Builds a hierarchical tree structure from a flat list of tree nodes. + * + * @param array $flatList + * A flat list of tree nodes; each node is an array with keys. + * + * @return array + * The hierarchical tree structure. */ -function build_catalog_tree(array $flatList) -{ - $grouped = []; - foreach ($flatList as $node){ - $grouped[$node['parent_id']][] = $node; +function vocabulary_catalog_build_catalog_tree(array $flatList) { + $grouped = []; + foreach ($flatList as $node) { + $grouped[$node['parent_id']][] = $node; + } + + $fnBuilder = function ($siblings) use (&$fnBuilder, $grouped) { + foreach ($siblings as $k => $sibling) { + $id = $sibling['id']; + if (isset($grouped[$id])) { + $sibling['children'] = $fnBuilder($grouped[$id]); + } + $siblings[$k] = $sibling; } + return $siblings; + }; - $fnBuilder = function($siblings) use (&$fnBuilder, $grouped) { - foreach ($siblings as $k => $sibling) { - $id = $sibling['id']; - if(isset($grouped[$id])) { - $sibling['children'] = $fnBuilder($grouped[$id]); - } - $siblings[$k] = $sibling; - } - return $siblings; - }; - - return $fnBuilder($grouped[0]); + return $fnBuilder($grouped[0]); } /** -* Implements hook_theme(). -*/ - + * Implements hook_theme(). + */ function vocabulary_catalog_theme($existing, $type, $theme, $path) { - return [ - 'vocabulary_catalog_block' => [ - 'variables' => ['content' => NULL], - ] - ]; -} \ No newline at end of file + return [ + 'vocabulary_catalog_block' => [ + 'variables' => ['content' => NULL], + ], + ]; +}