public function SocialTaggingService::buildHierarchy in Open Social 8.2
Same name and namespace in other branches
- 8.9 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8.3 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8.4 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8.5 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8.6 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8.7 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 8.8 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 10.3.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 10.0.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 10.1.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
- 10.2.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
Returns a multilevel tree.
Parameters
array $terms: An array of items that are selected.
Return value
array An hierarchy array of items with their parent.
File
- modules/
social_features/ social_tagging/ src/ SocialTaggingService.php, line 147
Class
- SocialTaggingService
- Provides a custom tagging service.
Namespace
Drupal\social_taggingCode
public function buildHierarchy(array $terms) {
$tree = [];
foreach ($terms as $term) {
if (!isset($term['target_id'])) {
continue;
}
$current_term = $this->termStorage
->load($term['target_id']);
// Must be a valid Term.
if (!$current_term instanceof TermInterface) {
continue;
}
// Get current terms parents.
$parents = $this->termStorage
->loadParents($current_term
->id());
$parent = reset($parents);
$category = $parent
->getName();
if ($this
->allowSplit()) {
$parameter = social_tagging_to_machine_name($category);
}
else {
$parameter = 'tag';
}
$url = Url::fromRoute('view.search_content.page_no_value', [
$parameter . '[]' => $current_term
->id(),
]);
$tree[$parent
->id()]['title'] = $category;
$tree[$parent
->id()]['tags'][$current_term
->id()] = [
'url' => $url
->toString(),
'name' => $current_term
->getName(),
];
}
// Return the tree.
return $tree;
}