Open Data Catalog v2.0.0
FacetsFromIndexTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\search_api\IndexInterface;
6 use Drupal\search_api\Query\QueryInterface;
7 
14  use FacetsCommonTrait;
15 
29  private function getFacetsFromIndex(array $params, IndexInterface $index, QueryInterface $query): array {
30  $fields = array_keys($index->getFields());
31  $facetsTypes = $this->getFacetsTypes($params);
32  $facets = [];
33  foreach ($facetsTypes as $type) {
34  if (in_array($type, $fields)) {
35  $facets = array_merge($facets, $this->getFacetsForType($type, $query));
36  }
37  }
38  return $facets;
39  }
40 
52  private function getFacetsForType(string $type, QueryInterface $query) {
53  $facets = [];
54  $field = '';
55 
56  // Prepare facets for fields that correspond to objects.
57  $matches = [];
58  if (preg_match('/(.*)__(.*)/', $type, $matches)) {
59  $schema = $matches[1];
60  $field = $matches[2];
61  }
62  else {
63  $schema = $type;
64  }
65  foreach ($this->metastoreService->getAll($schema) as $collection) {
66  $facet_name = empty($field) ? $collection->{'$.data'} : $collection->{'$.data.' . $field};
67  $facets[] = $this->getFacet($type, $facet_name, $query);
68  }
69 
70  return $facets;
71  }
72 
86  private function getFacet(string $type, string $facetName, QueryInterface $query): object {
87  $cloneQuery = clone $query;
88  $cloneQuery->addCondition($type, $facetName);
89  $result = $cloneQuery->execute();
90  return (object) [
91  'type' => $type,
92  'name' => $facetName,
93  'total' => $result->getResultCount(),
94  ];
95  }
96 
97 }
Drupal\metastore_search\FacetsFromIndexTrait
trait FacetsFromIndexTrait
Definition: FacetsFromIndexTrait.php:13
Drupal\metastore_search