You are here

public function SocialTaggingService::buildHierarchy in Open Social 8.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 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.4 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  6. 8.5 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  7. 8.6 modules/social_features/social_tagging/src/SocialTaggingService.php \Drupal\social_tagging\SocialTaggingService::buildHierarchy()
  8. 8.7 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 $term_ids: 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 195

Class

SocialTaggingService
Provides a custom tagging service.

Namespace

Drupal\social_tagging

Code

public function buildHierarchy(array $term_ids, $entity_type) {
  $tree = [];

  // Load all the terms together.
  if (!empty($terms = $this->termStorage
    ->loadMultiple(array_column($term_ids, 'target_id')))) {

    // Get current language.
    // This is used to get the translated term, if available.
    $langcode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();

    // Get splitting of fields option.
    $allowSplit = $this
      ->allowSplit();

    // Set the route.
    $route = $entity_type == 'group' ? 'view.search_groups.page_no_value' : 'view.search_content.page_no_value';

    // Build the hierarchy.
    foreach ($terms as $current_term) {

      // 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_label = $parent
        ->hasTranslation($langcode) ? $parent
        ->getTranslation($langcode)
        ->getName() : $parent
        ->getName();

      // Prepare the parameter;.
      $parameter = $allowSplit ? social_tagging_to_machine_name($category_label) : 'tag';

      // Prepare the URL for the search by term.
      $url = Url::fromRoute($route, [
        $parameter . '[]' => $current_term
          ->id(),
      ])
        ->toString();

      // Finally, prepare the hierarchy.
      $tree[$parent
        ->id()]['title'] = $category_label;
      $tree[$parent
        ->id()]['tags'][$current_term
        ->id()] = [
        'url' => $url,
        'name' => $current_term
          ->hasTranslation($langcode) ? $current_term
          ->getTranslation($langcode)
          ->getName() : $current_term
          ->getName(),
      ];
    }
  }

  // Return the tree.
  return $tree;
}