public function Vocabulary::view in Sitemap 2.0.x
Same name and namespace in other branches
- 8.2 src/Plugin/Sitemap/Vocabulary.php \Drupal\sitemap\Plugin\Sitemap\Vocabulary::view()
Builds a renderable array for a sitemap item.
Return value
array A renderable array for a themed field with its label and all its values.
Overrides SitemapInterface::view
File
- src/
Plugin/ Sitemap/ Vocabulary.php, line 194
Class
- Vocabulary
- Provides a sitemap for an taxonomy vocabulary.
Namespace
Drupal\sitemap\Plugin\SitemapCode
public function view() {
$vid = $this->pluginDefinition['vocabulary'];
/** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
$vocabulary = \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary')
->load($vid);
$content = [];
if (isset($this->settings['show_description']) && $this->settings['show_description']) {
$content[] = [
'#markup' => $vocabulary
->getDescription(),
];
}
// Plan for a nested list of terms.
$list = [];
if ($maxDepth = $this->settings['term_depth']) {
/** @var \Drupal\taxonomy\TermStorageInterface $termStorage */
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$hierarchyType = $termStorage
->getVocabularyHierarchyType($vid);
// Fetch the top-level terms.
$terms = $termStorage
->loadTree($vid, 0, 1);
// We might not need to worry about the vocabulary being nested.
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) {
// Use a more structured tree to create a nested list.
foreach ($terms as $obj) {
$currentDepth = 1;
$this
->buildList($list, $obj, $vid, $currentDepth, $maxDepth);
// TODO: Remove parents where all child terms are not displayed.
}
}
else {
// TODO: Support multiple hierarchy? Need to test
}
}
// @TODO: Test & Document
// Add an alter hook for modules to manipulate the taxonomy term output.
\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,
];
}