You are here

public function SocialProfileTagService::buildHierarchy in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_profile/src/SocialProfileTagService.php \Drupal\social_profile\SocialProfileTagService::buildHierarchy()

Returns a multilevel tree.

Parameters

array $term_ids: An array of items that are selected.

Return value

array An hierarchy array of items with their parent.

Overrides SocialProfileTagServiceInterface::buildHierarchy

File

modules/social_features/social_profile/src/SocialProfileTagService.php, line 157

Class

SocialProfileTagService
Provide a service for profile tagging.

Namespace

Drupal\social_profile

Code

public function buildHierarchy(array $term_ids) {
  $tree = [];
  $terms = $this->taxonomyStorage
    ->loadMultiple(array_column($term_ids, 'target_id'));
  if (empty($terms)) {
    return [];
  }
  foreach ($terms as $term) {
    if (!$term instanceof TermInterface) {
      continue;
    }
    $parents = $this->taxonomyStorage
      ->loadParents($term
      ->id());
    if ($parents) {
      $parent = reset($parents);
    }
    else {
      $parent = $term;
    }
    $parent_label = $parent
      ->getName();
    $route = 'view.search_users.page_no_value';
    $route_parameters = [
      'created_op' => '<',
      'profile_tag[]' => $term
        ->id(),
    ];

    // Prepare the URL for the search by term.
    $url = Url::fromRoute($route, $route_parameters)
      ->toString();
    $tree[$parent
      ->id()]['title'] = $parent_label;
    $tree[$parent
      ->id()]['tags'][$term
      ->id()] = [
      'url' => $url,
      'name' => $term
        ->getName(),
    ];
  }
  return $tree;
}