You are here

protected function SearchApiAlgoliaBackend::prepareItem in Search API Algolia 8

Same name and namespace in other branches
  1. 3.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::prepareItem()
  2. 2.0.x src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php \Drupal\search_api_algolia\Plugin\search_api\backend\SearchApiAlgoliaBackend::prepareItem()

Prepares a single item for indexing.

Used as a helper method in indexItem()/indexItems().

Parameters

\Drupal\search_api\IndexInterface $index: Index.

\Drupal\search_api\Item\ItemInterface $item: The item to index.

1 call to SearchApiAlgoliaBackend::prepareItem()
SearchApiAlgoliaBackend::indexItems in src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php
Indexes the specified items.

File

src/Plugin/search_api/backend/SearchApiAlgoliaBackend.php, line 254

Class

SearchApiAlgoliaBackend
Class SearchApiAlgoliaBackend.

Namespace

Drupal\search_api_algolia\Plugin\search_api\backend

Code

protected function prepareItem(IndexInterface $index, ItemInterface $item) {
  $item_id = $item
    ->getId();
  $item_to_index = [
    'objectID' => $item_id,
  ];

  /** @var \Drupal\search_api\Item\FieldInterface $field */
  $item_fields = $item
    ->getFields();
  $item_fields += $this
    ->getSpecialFields($index, $item);
  foreach ($item_fields as $field) {
    $type = $field
      ->getType();
    $values = NULL;
    $field_values = $field
      ->getValues();
    if (empty($field_values)) {
      continue;
    }
    foreach ($field_values as $field_value) {
      if (!$field_value) {
        continue;
      }
      switch ($type) {
        case 'text':
        case 'string':
        case 'uri':
          $field_value .= '';
          if (mb_strlen($field_value) > 10000) {
            $field_value = mb_substr(trim($field_value), 0, 10000);
          }
          $values[] = $field_value;
          break;
        case 'integer':
        case 'duration':
        case 'decimal':
          $values[] = 0 + $field_value;
          break;
        case 'boolean':
          $values[] = $field_value ? TRUE : FALSE;
          break;
        case 'date':
          if (is_numeric($field_value) || !$field_value) {
            $values[] = 0 + $field_value;
            break;
          }
          $values[] = strtotime($field_value);
          break;
        default:
          $values[] = $field_value;
      }
    }
    if (is_array($values) && count($values) <= 1) {
      $values = reset($values);
    }
    $item_to_index[$field
      ->getFieldIdentifier()] = $values;
  }
  return $item_to_index;
}