You are here

public function SocialTaggingService::buildHierarchy in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  2. 8.2 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  3. 8.3 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  4. 8.4 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  5. 8.5 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  6. 8.6 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  7. 8.7 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  8. 8.8 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  9. 10.3.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  10. 10.0.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  11. 10.1.x modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  12. 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_tagging

Code

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;
}