You are here

public function DateRange::preprocessIndexItems in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/processor/DateRange.php \Drupal\search_api_solr\Plugin\search_api\processor\DateRange::preprocessIndexItems()
  2. 8.2 src/Plugin/search_api/processor/DateRange.php \Drupal\search_api_solr\Plugin\search_api\processor\DateRange::preprocessIndexItems()

File

src/Plugin/search_api/processor/DateRange.php, line 27

Class

DateRange
Add date ranges to the index.

Namespace

Drupal\search_api_solr\Plugin\search_api\processor

Code

public function preprocessIndexItems(array $items) {
  foreach ($items as $item) {

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    foreach ($item
      ->getFields() as $field) {
      if ('solr_date_range' === $field
        ->getType()) {
        $values = [];
        $required_properties = [
          $item
            ->getDatasourceId() => [
            $field
              ->getPropertyPath() . ':value' => 'start',
            $field
              ->getPropertyPath() . ':end_value' => 'end',
          ],
        ];
        $item_values = $this
          ->getFieldsHelper()
          ->extractItemValues([
          $item,
        ], $required_properties);
        foreach ($item_values as $dates) {
          $start_dates = $dates['start'];
          $end_dates = $dates['end'];
          for ($i = 0, $n = count($start_dates); $i < $n; $i++) {
            $values[$i] = new DateRangeValue($start_dates[$i], $end_dates[$i]);
          }
        }
        if (!empty($values)) {
          $field
            ->setValues($values);
        }
      }
    }
  }
}