Commit 3034bb50 authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #2802571 by mkalkbrenner, DanielVeza, ekes: Hierarchy date facet: Year, Year-Month etc

parent 0c1e557b
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\facets\Plugin\facets\hierarchy;

use Drupal\facets\Hierarchy\HierarchyPluginBase;

/**
 * Provides the DateItems hierarchy class.
 *
 * @FacetsHierarchy(
 *   id = "date_items",
 *   label = @Translation("Date item hierarchy"),
 *   description = @Translation("Display hierarchical dates."),
 * )
 */
class DateItems extends HierarchyPluginBase {

  /**
   * Static cache for the parents.
   *
   * @var array
   */
  protected $parents = [];

  /**
   * Static cache for the children.
   *
   * @var array
   */
  protected $children = [];

  /**
   * {@inheritdoc}
   */
  public function getParentIds($id) {
    if (preg_match('/(.*)[-T:]\d+$/', $id, $matches)) {
      $this->parents[$id] = $matches[1];
      $this->children[$matches[1]][] = $id;
      return [$matches[1]];
    }

    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getNestedChildIds($id) {
    $nested_children = [];
    if (isset($this->children[$id])) {
      foreach ($this->children[$id] as $child) {
        $nested_children[] = $child;
        $nested_children = array_merge($nested_children, $this->getNestedChildIds($child));
      }
    }

    return $nested_children;
  }

  /**
   * {@inheritdoc}
   */
  public function getChildIds(array $ids) {
    return array_intersect_key($this->children, array_flip($ids));
  }

}
+9 −0
Original line number Diff line number Diff line
@@ -68,6 +68,14 @@ class DateItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
      '#default_value' => $this->getConfiguration()['granularity'],
      '#options' => $this->granularityOptions(),
    ];

    $build['hierarchy'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Hierarchy'),
      '#default_value' => $this->getConfiguration()['hierarchy'],
      '#description' => $this->t('Create a hierarchical facet instead of a flat list. It is important to also activate "use hierarchy" and to select "date item hierarchy" as hierarchy type.'),
    ];

    $build['date_format'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Date format'),
@@ -95,6 +103,7 @@ class DateItemProcessor extends ProcessorPluginBase implements BuildProcessorInt
    return [
      'date_display' => 'actual_date',
      'granularity' => SearchApiDate::FACETAPI_DATE_MONTH,
      'hierarchy' => FALSE,
      'date_format' => '',
    ];
  }
+52 −7
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ class SearchApiDate extends QueryTypeRangeBase {

    $configuration = $this->getConfiguration();
    $configuration['granularity'] = $dateProcessorConfig['granularity'];
    $configuration['hierarchy'] = $dateProcessorConfig['hierarchy'];
    $configuration['date_display'] = $dateProcessorConfig['date_display'];
    $configuration['date_format'] = $dateProcessorConfig['date_format'];
    $this->setConfiguration($configuration);
@@ -73,12 +74,41 @@ class SearchApiDate extends QueryTypeRangeBase {
   * {@inheritdoc}
   */
  public function calculateRange($value) {
    $counts = count_chars($value, 1);
    $granularity = self::FACETAPI_DATE_YEAR - ($counts[ord('-')] ?? 0) - ($counts[ord('T')] ?? 0) - ($counts[ord(':')] ?? 0);

    if ($this->getDateDisplay() === 'relative_date') {
      return $this->calculateRangeRelative($value);
      return $this->calculateRangeRelative($value, $granularity);
    }
    else {
      return $this->calculateRangeAbsolute($value);

    return $this->calculateRangeAbsolute($value, $granularity);
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $granularity = $this->getGranularity();

    if (!$this->getHierarchy() || $granularity === self::FACETAPI_DATE_YEAR) {
      return parent::build();
    }

    $configuration = $this->getConfiguration();
    $facet_results= [];

    while ($this->getGranularity() <= self::FACETAPI_DATE_YEAR) {
      parent::build();
      $facet_results += $this->facet->getResults();
      $configuration['granularity'] = $this->getGranularity() + 1;
      $this->setConfiguration($configuration);
    }

    $configuration['granularity'] = $granularity;
    $this->setConfiguration($configuration);

    $this->facet->setResults($facet_results);
    return $this->facet;
  }

  /**
@@ -89,6 +119,8 @@ class SearchApiDate extends QueryTypeRangeBase {
   *
   * @param int $value
   *   Unix timestamp.
   * @param int $granularity
   *   The grnaularity.
   *
   * @return array
   *   An array with a start and end date as unix timestamps.
@@ -96,10 +128,10 @@ class SearchApiDate extends QueryTypeRangeBase {
   * @throws \Exception
   *   Thrown when creating a date fails.
   */
  protected function calculateRangeAbsolute($value) {
  protected function calculateRangeAbsolute($value, $granularity) {
    $dateTime = new DrupalDateTime();

    switch ($this->getGranularity()) {
    switch ($granularity) {
      case static::FACETAPI_DATE_YEAR:
        $startDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value . '-01-01T00:00:00');
        $stopDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value . '-12-31T23:59:59');
@@ -145,6 +177,9 @@ class SearchApiDate extends QueryTypeRangeBase {
   *
   * @param int $value
   *   Unix timestamp.
   * @param int $granularity
   *   The grnaularity.
   *
   *
   * @return array
   *   An array with a start and end date as unix timestamps.
@@ -152,10 +187,10 @@ class SearchApiDate extends QueryTypeRangeBase {
   * @throws \Exception
   *   Thrown when creating a date fails.
   */
  protected function calculateRangeRelative($value) {
  protected function calculateRangeRelative($value, int $granularity) {
    $dateTime = new DrupalDateTime();

    switch ($this->getGranularity()) {
    switch ($granularity) {
      case static::FACETAPI_DATE_YEAR:
        $startDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value . '-01T00:00:00');
        $stopDate = clone $startDate;
@@ -418,6 +453,16 @@ class SearchApiDate extends QueryTypeRangeBase {
    return $this->getConfiguration()['granularity'];
  }

  /**
   * Retrieve configuration: Hierarchy.
   *
   * @return bool
   *   The hierarchy for this config.
   */
  protected function getHierarchy() {
    return $this->getConfiguration()['hierarchy'];
  }

  /**
   * Retrieve configuration: Date Display type.
   *