View source
<?php
namespace Drupal\sitemap\Plugin\Sitemap;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\sitemap\SitemapBase;
use Drupal\taxonomy\VocabularyInterface;
class Vocabulary extends SitemapBase {
const DEPTH_MAX = 9;
const DEPTH_DISABLED = 0;
const THRESHOLD_DISABLED = 0;
const DEFAULT_TERM_LINK = 'entity.taxonomy_term.canonical|taxonomy_term';
const DEFAULT_TERM_RSS_LINK = 'view.taxonomy_term.feed_1|arg_0';
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$vid = $this
->getPluginDefinition()['vocabulary'];
$vocab = \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary')
->load($vid);
$form['title']['#default_value'] = $this->settings['title'] ?: $vocab
->label();
$form['show_description'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display vocabulary description'),
'#default_value' => $this->settings['show_description'],
'#description' => $this
->t('When enabled, this option will show the vocabulary description.'),
];
$form['show_count'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Display node counts next to taxonomy terms'),
'#default_value' => $this->settings['show_count'],
'#description' => $this
->t('When enabled, this option will show the number of nodes in each taxonomy term.'),
];
$form['term_depth'] = [
'#type' => 'textfield',
'#title' => $this
->t('Term depth'),
'#default_value' => $this->settings['term_depth'],
'#size' => 3,
'#description' => $this
->t('Specify how many levels of taxonomy terms should be included. For instance, enter <code>1</code> to only include top-level terms, or <code>@disabled</code> to include no terms. The maximum depth is <code>@max</code>.', [
'@disabled' => self::DEPTH_DISABLED,
'@max' => self::DEPTH_MAX,
]),
];
$form['term_count_threshold'] = [
'#type' => 'textfield',
'#title' => $this
->t('Term count threshold'),
'#default_value' => $this->settings['term_count_threshold'],
'#size' => 3,
'#description' => $this
->t('Only show taxonomy terms whose node counts are greater than this threshold. Set to <em>@disabled</em> to disable this threshold. Note that in hierarchical taxonomies, parent items with children will still be shown.', [
'@disabled' => self::THRESHOLD_DISABLED,
]),
];
$form['customize_link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Customize term links'),
'#default_value' => $this->settings['customize_link'],
];
$customizeLinkName = 'plugins[vocabulary:' . $vid . '][settings][customize_link]';
$form['term_link'] = [
'#type' => 'textfield',
'#title' => $this
->t('Term link route and argument'),
'#default_value' => $this->settings['term_link'],
"#description" => $this
->t('Provide the route name and route argument name for the link, in the from of <code>route.name|argument.name</code>. The default value of this field is <code>entity.taxonomy_term.canonical|taxonomy_term</code>.'),
'#states' => [
'visible' => [
':input[name="' . $customizeLinkName . '"]' => [
'checked' => TRUE,
],
],
],
];
$form['always_link'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Always link to the taxonomy term.'),
'#default_value' => $this->settings['always_link'],
'#description' => $this
->t('There are a few cases where a taxonomy term maybe be displayed in the list, but will not have a link created (for example, terms without any tagged content [nodes], or parent terms displayed when the threshold is greater than zero). Check this box to ensure that a link to the taxonomy term is always provided.'),
'#states' => [
'visible' => [
':input[name="' . $customizeLinkName . '"]' => [
'checked' => TRUE,
],
],
],
];
$form['enable_rss'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable RSS feed links'),
'#default_value' => $this->settings['enable_rss'],
];
$enableRssName = 'plugins[vocabulary:' . $vid . '][settings][enable_rss]';
$form['rss_link'] = [
'#type' => 'textfield',
'#title' => $this
->t('RSS route and argument'),
'#default_value' => $this->settings['rss_link'],
"#description" => $this
->t('Provide the route name and route argument name for the link, in the from of <code>route.name|argument.name</code>. The default value of this field is <code>view.taxonomy_term.feed_1|arg_0</code>.'),
'#states' => [
'visible' => [
':input[name="' . $enableRssName . '"]' => [
'checked' => TRUE,
],
],
],
];
$form['rss_depth'] = [
'#type' => 'textfield',
'#title' => $this
->t('RSS depth'),
'#default_value' => $this->settings['rss_depth'],
'#size' => 3,
'#maxlength' => 10,
'#description' => $this
->t('Specify how many levels of taxonomy terms should have a link to the default RSS feed included. For instance, enter <code>1</code> to include an RSS feed for the top-level terms, or <code>@disabled</code> to not include a feed. The maximum depth is <code>@max</code>.', [
'@disabled' => self::DEPTH_DISABLED,
'@max' => self::DEPTH_MAX,
]),
'#states' => [
'visible' => [
':input[name="' . $enableRssName . '"]' => [
'checked' => TRUE,
],
],
],
];
return $form;
}
public function view() {
$vid = $this->pluginDefinition['vocabulary'];
$vocabulary = \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary')
->load($vid);
$content = [];
if (isset($this->settings['show_description']) && $this->settings['show_description']) {
$content[] = [
'#markup' => $vocabulary
->getDescription(),
];
}
$list = [];
if ($maxDepth = $this->settings['term_depth']) {
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$hierarchyType = $termStorage
->getVocabularyHierarchyType($vid);
$terms = $termStorage
->loadTree($vid, 0, 1);
if ($hierarchyType == VocabularyInterface::HIERARCHY_DISABLED || $maxDepth == 1) {
foreach ($terms as $term) {
$term->treeDepth = $term->depth;
if ($display = $this
->buildSitemapTerm($term)) {
$list[$term->tid]['data'] = $display;
}
}
}
elseif ($hierarchyType == VocabularyInterface::HIERARCHY_SINGLE) {
foreach ($terms as $obj) {
$currentDepth = 1;
$this
->buildList($list, $obj, $vid, $currentDepth, $maxDepth);
}
}
else {
}
}
\Drupal::moduleHandler()
->alter([
'sitemap_vocabulary',
'sitemap_vocabulary_' . $vid,
], $list, $vid);
$content[] = [
'#theme' => 'item_list',
'#items' => $list,
];
return [
'#theme' => 'sitemap_item',
'#title' => $this->settings['title'],
'#content' => $content,
'#sitemap' => $this,
];
}
protected function buildSitemapTerm($term) {
$this
->checkTermThreshold($term);
if ($term->display) {
return [
'#theme' => 'sitemap_taxonomy_term',
'#name' => $term->name,
'#url' => $this
->buildTermLink($term) ?: '',
'#show_link' => $this
->determineLinkVisibility($term),
'#show_count' => $this
->determineCountVisibility($term),
'#count' => isset($term->count) ? $term->count : '',
'#show_feed' => $this->settings['enable_rss'],
'#feed' => $this
->buildFeedLink($term) ?: '',
];
}
}
protected function checkTermThreshold(&$term) {
if (!isset($term->display)) {
$term->display = FALSE;
}
$threshold = $this->settings['term_count_threshold'];
$showCount = $this->settings['show_count'];
$term->count = sitemap_taxonomy_term_count_nodes($term->tid);
if ($threshold || $showCount) {
if ($threshold && !isset($term->hasChildren)) {
if ($term->count >= $threshold) {
$term->display = TRUE;
}
}
else {
$term->display = TRUE;
}
}
else {
$term->display = TRUE;
}
}
protected function buildTermLink($term) {
$vid = $this->pluginDefinition['vocabulary'];
if (\Drupal::service('module_handler')
->moduleExists('forum') && $vid == \Drupal::config('forum.settings')
->get('vocabulary')) {
return Url::fromRoute('forum.index')
->toString();
}
if (isset($this->settings['term_link'])) {
return $this
->buildLink($this->settings['term_link'], $term->tid);
}
}
protected function buildFeedLink($term) {
$rssDepth = $this->settings['rss_depth'];
if ($rssDepth && isset($term->treeDepth) && $rssDepth >= $term->treeDepth) {
if (isset($this->settings['rss_link'])) {
return $this
->buildLink($this->settings['rss_link'], $term->tid);
}
}
}
protected function buildList(&$list, $object, $vid, &$currentDepth, $maxDepth) {
if ($object->depth != 0) {
return;
}
$object->treeDepth = $currentDepth;
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$children = $termStorage
->loadChildren($object->tid);
if (!$children) {
$object->hasChildren = FALSE;
if ($element = $this
->buildSitemapTerm($object)) {
$list[$object->tid][] = $element;
}
return;
}
else {
$object->display = TRUE;
$object->hasChildren = TRUE;
$list[$object->tid][] = $this
->buildSitemapTerm($object);
$list[$object->tid]['children'] = [];
$object_children =& $list[$object->tid]['children'];
}
$currentDepth++;
if ($maxDepth >= $currentDepth) {
$child_objects = $termStorage
->loadTree($vid, $object->tid, 1);
foreach ($children as $child) {
foreach ($child_objects as $child_object) {
if ($child_object->tid == $child
->id()) {
$this
->buildlist($object_children, $child_object, $vid, $currentDepth, $maxDepth);
}
}
}
}
}
protected function determineLinkVisibility($term) {
if ($this->settings['always_link']) {
return TRUE;
}
elseif ($this->settings['term_count_threshold'] == Vocabulary::THRESHOLD_DISABLED && $term->count) {
return TRUE;
}
elseif ($this->settings['term_count_threshold'] && $term->count >= $this->settings['term_count_threshold']) {
return TRUE;
}
return FALSE;
}
protected function determineCountVisibility($term) {
if ($this->settings['show_count']) {
if ($threshold = $this->settings['term_count_threshold']) {
if ($term->count >= $threshold) {
return TRUE;
}
}
else {
return TRUE;
}
}
return FALSE;
}
protected function buildLink($string, $tid) {
$parts = $this
->_splitRouteArg($string);
return Url::fromRoute($parts['route'], [
$parts['arg'] => $tid,
])
->toString();
}
protected function _splitRouteArg($string) {
$return = [];
if ($string) {
$arr = explode('|', $string);
if (count($arr) == 2) {
$return['route'] = $arr[0];
$return['arg'] = $arr[1];
}
}
return $return;
}
protected function validateCustomRoute($string) {
$parts = $this
->_splitRouteArg($string);
$route_provider = \Drupal::service('router.route_provider');
try {
$route = $route_provider
->getRouteByName($parts['route']);
} catch (\Exception $e) {
}
}
}