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