You are here

public function Taxonomy::getSiblingIds in Facets 8

Provide a default implementation for backward compatibility.

Overrides HierarchyPluginBase::getSiblingIds

File

src/Plugin/facets/hierarchy/Taxonomy.php, line 120

Class

Taxonomy
Taxonomy hierarchy.

Namespace

Drupal\facets\Plugin\facets\hierarchy

Code

public function getSiblingIds(array $ids, array $activeIds = [], bool $parentSiblings = TRUE) {
  if (empty($ids)) {
    return [];
  }
  $parentIds = [];
  $topLevelTerms = [];
  foreach ($ids as $id) {
    if (!$activeIds || in_array($id, $activeIds)) {
      $currentParentIds = $this
        ->getParentIds($id);
      if (!$currentParentIds) {
        if (!$topLevelTerms) {

          /** @var \Drupal\taxonomy\Entity\Term $term */
          $term = $this->termStorage
            ->load($id);
          $topLevelTerms = array_map(function ($term) {
            return $term->tid;
          }, $this->termStorage
            ->loadTree($term
            ->bundle(), 0, 1));
        }
      }
      else {
        $parentIds[] = $currentParentIds;
      }
    }
  }
  $parentIds = array_unique(array_merge([], ...$parentIds));
  $childIds = array_merge([], ...$this
    ->getChildIds($parentIds));
  return array_diff(array_merge($childIds, $topLevelTerms, !$topLevelTerms && $parentSiblings ? $this
    ->getSiblingIds($ids, $parentIds) : []), $ids);
}