You are here

public function SocialTaggingService::buildHierarchy in Open Social 8.4

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 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  3. 8.2 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  4. 8.3 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.

string $entity_type: The entity type these tags are for.

Return value

array An hierarchy array of items with their parent.

File

modules/social_features/social_tagging/src/SocialTaggingService.php, line 160

Class

SocialTaggingService
Provides a custom tagging service.

Namespace

Drupal\social_tagging

Code

public function buildHierarchy(array $terms, $entity_type) {
  $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();
    $parameter = 'tag';
    if ($this
      ->allowSplit()) {
      $parameter = social_tagging_to_machine_name($category);
    }
    $route = 'view.search_content.page_no_value';
    if ($entity_type == 'group') {
      $route = 'view.search_groups.page_no_value';
    }
    $url = Url::fromRoute($route, [
      $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;
}