View source
<?php
namespace Drupal\site_map;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Url;
class SiteMapHelper {
protected $configFactory;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
public function setOption(array &$options, $option_string, $equal_param, $set_string, $set_value) {
$config = $this->configFactory
->get('site_map.settings');
if ($config
->get($option_string) == $equal_param) {
$options[$set_string] = $set_value;
}
}
public function getTerms($voc) {
$output = '';
$options = array();
if (\Drupal::moduleHandler()
->moduleExists('taxonomy') && !empty($voc)) {
if (\Drupal::moduleHandler()
->moduleExists('i18n_taxonomy')) {
$voc->name = i18n_taxonomy_vocabulary_name($voc, $GLOBALS['language']->language);
}
$output = $this
->getTaxonomyTree($voc
->get('vid'), $voc
->get('name'), $voc
->get('description'));
$this
->setOption($options, 'show_titles', 1, 'show_titles', TRUE);
}
return $output;
}
public function getTaxonomyTree($vid, $name = NULL, $description = NULL) {
$output = '';
$options = array();
$class = array();
$config = \Drupal::config('site_map.settings');
if (\Drupal::service('module_handler')
->moduleExists('forum') && $vid == \Drupal::config('forum.settings')
->get('forum_nav_vocabulary')) {
$title = \Drupal::l($name, Url::fromRoute('forum.index'));
$threshold = $config
->get('forum_threshold');
$forum_link = TRUE;
}
else {
$title = $name;
$threshold = $config
->get('term_threshold');
$forum_link = FALSE;
}
if (\Drupal::service('module_handler')
->moduleExists('commentrss') && \Drupal::config('commentrss.settings')
->get('commentrss_term')) {
$feed_icon = array(
'#theme' => 'site_map_feed_icon',
'#url' => "crss/vocab/{$vid}",
'#name' => $name,
'#type' => 'comment',
);
$title .= ' ' . drupal_render($feed_icon);
}
$last_depth = -1;
$output .= !empty($description) && $config
->get('show_description') ? '<div class="description">' . Xss::filterAdmin($description) . "</div>\n" : '';
$depth = $config
->get('vocabulary_depth');
if ($depth <= -1) {
$depth = NULL;
}
$tree = \Drupal::entityManager()
->getStorage('taxonomy_term')
->loadTree($vid, 0, $depth);
foreach ($tree as $term) {
$term->count = site_map_taxonomy_term_count_nodes($term->tid);
if ($term->count <= $threshold) {
continue;
}
if (\Drupal::service('module_handler')
->moduleExists('i18n_taxonomy')) {
$term->name = i18n_taxonomy_term_name($term, $GLOBALS['language']->language);
}
if ($term->depth > $last_depth) {
for ($i = 0; $i < $term->depth - $last_depth; $i++) {
$output .= "\n<ul>";
}
}
elseif ($term->depth == $last_depth) {
$output .= '</li>';
}
elseif ($term->depth < $last_depth) {
for ($i = 0; $i < $last_depth - $term->depth; $i++) {
$output .= "</li>\n</ul>\n</li>";
}
}
$output .= "\n<li>";
$term_item = '';
if ($forum_link) {
$term_item .= \Drupal::l($term->name, Url::fromRoute('forum.page', array(
'taxonomy_term' => $term->tid,
), array(
'attributes' => array(
'title' => $term->description__value,
),
)));
}
elseif ($term->count) {
$term_item .= \Drupal::l($term->name, Url::fromRoute('entity.taxonomy_term.canonical', array(
'taxonomy_term' => $term->tid,
), array(
'attributes' => array(
'title' => $term->description__value,
),
)));
}
else {
$term_item .= $term->name;
}
if ($config
->get('show_count')) {
$span_title = '';
$term_item .= " <span title=\"" . $span_title . "\">(" . $term->count . ")</span>";
}
$rss_depth = $config
->get('rss_taxonomy');
if ($config
->get('show_rss_links') != 0 && ($rss_depth == -1 || $term->depth < $rss_depth)) {
$feed_icon = array(
'#theme' => 'site_map_feed_icon',
'#url' => 'taxonomy/term/' . $term->tid . '/feed',
'#name' => $term->name,
);
$rss_link = drupal_render($feed_icon);
if (\Drupal::service('module_handler')
->moduleExists('commentrss') && \Drupal::config('commentrss.settings')
->get('commentrss_term')) {
$feed_icon = array(
'#theme' => 'site_map_feed_icon',
'#url' => "crss/term/{$term->tid}",
'#type' => 'comment',
'#name' => $term->name . ' comments',
);
$rss_link .= drupal_render($feed_icon);
}
if ($config
->get('show_rss_links') == 1) {
$term_item .= ' ' . $rss_link;
}
else {
$class[] = 'site-map-rss-left';
$term_item = $rss_link . ' ' . $term_item;
}
}
\Drupal::moduleHandler()
->alter(array(
'site_map_taxonomy_term',
'site_map_taxonomy_term_' . $term->tid,
), $term_item, $term);
$output .= $term_item;
$last_depth = $term->depth;
}
if ($last_depth > -1) {
for ($i = 0; $i < $last_depth + 1; $i++) {
$output .= "</li>\n</ul>\n";
}
}
$this
->setOption($options, 'show_titles', 1, 'show_titles', TRUE);
$class[] = 'site-map-box-terms';
$class[] = 'site-map-box-terms-' . $vid;
$attributes = array(
'class' => $class,
);
$site_map_box = array(
'#theme' => 'site_map_box',
'#title' => $title,
'#content' => array(
'#markup' => $output,
),
'#attributes' => $attributes,
'#options' => $options,
);
return $site_map_box;
}
}