You are here

function apachesolr_date_default_indexing_callback in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 apachesolr.index.inc \apachesolr_date_default_indexing_callback()
  2. 7 apachesolr.index.inc \apachesolr_date_default_indexing_callback()

This function is used during indexing to normalize the DATE and DATETIME fields into the appropriate format for Apache Solr.

1 string reference to 'apachesolr_date_default_indexing_callback'
date_apachesolr_field_mappings in ./apachesolr.module
Implements hook_apachesolr_field_mappings() on behalf of date module.

File

./apachesolr.index.inc, line 1102
Functions related to Apache Solr indexing operations.

Code

function apachesolr_date_default_indexing_callback($entity, $field_name, $index_key, $field_info) {
  $fields = array();
  if (!empty($entity->{$field_name})) {
    $field = $entity->{$field_name};
    foreach ($field as $value) {

      // Construct a Solr-ready date string in UTC time zone based on the field's date string and time zone.
      $tz = new DateTimeZone(isset($value['timezone']) ? $value['timezone'] : 'UTC');

      // $fields may end up having two values; one for the start date
      // and one for the end date.
      if ($date = date_create($value['value'], $tz)) {
        $index_value = apachesolr_date_iso($date
          ->format('U'));
        $fields[] = array(
          'key' => $index_key,
          'value' => $index_value,
        );
        if (isset($value['value2'])) {
          if ($date = date_create($value['value2'], $tz)) {
            $index_value = apachesolr_date_iso($date
              ->format('U'));
            $fields[] = array(
              // The value2 element is the end date. Therefore it gets indexed
              // into its own Solr field.
              'key' => $index_key . '_end',
              'value' => $index_value,
            );
          }
        }
      }
    }
  }
  return $fields;
}