You are here

public function SearchApiElasticsearchBackend::getDateGranularity in Elasticsearch Connector 8.6

Same name and namespace in other branches
  1. 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getDateGranularity()
  2. 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getDateGranularity()
  3. 8.2 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getDateGranularity()
  4. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::getDateGranularity()

Helper function to return date gap.

Parameters

$adapter:

$facet_id:

Return value

mixed|string

1 call to SearchApiElasticsearchBackend::getDateGranularity()
SearchApiElasticsearchBackend::getDateFacetInterval in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Helper function which add params to date facets.

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 1035

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function getDateGranularity($adapter, $facet_id) {

  // Date gaps.
  $gap_weight = [
    'YEAR' => 2,
    'MONTH' => 1,
    'DAY' => 0,
  ];
  $gaps = [];
  $date_gap = 'YEAR';

  // Get the date granularity.
  if (isset($adapter)) {

    // Get the current date gap from the active date filters.
    $active_items = $adapter
      ->getActiveItems([
      'name' => $facet_id,
    ]);
    if (!empty($active_items)) {
      foreach ($active_items as $active_item) {
        $value = $active_item['value'];
        if (strpos($value, ' TO ') > 0) {
          list($date_min, $date_max) = explode(' TO ', str_replace([
            '[',
            ']',
          ], '', $value), 2);
          $gap = self::getDateGap($date_min, $date_max, FALSE);
          if (isset($gap_weight[$gap])) {
            $gaps[] = $gap_weight[$gap];
          }
        }
      }
      if (!empty($gaps)) {

        // Minimum gap.
        $date_gap = array_search(min($gaps), $gap_weight);
      }
    }
  }
  return $date_gap;
}