You are here

public function SearchApiFederatedSolrField::alterItems in Search API Federated Solr 7

Same name and namespace in other branches
  1. 7.3 src/SearchApiFederatedSolrField.php \SearchApiFederatedSolrField::alterItems()
  2. 7.2 src/SearchApiFederatedSolrField.php \SearchApiFederatedSolrField::alterItems()

Alter items before indexing.

Items which are removed from the array won't be indexed, but will be marked as clean for future indexing. This could for instance be used to implement some sort of access filter for security purposes (e.g., don't index unpublished nodes or comments).

Parameters

array $items: An array of items to be altered, keyed by item IDs.

Overrides SearchApiAlterCallbackInterface::alterItems

File

src/SearchApiFederatedSolrField.php, line 18

Class

SearchApiFederatedSolrField

Code

public function alterItems(array &$items) {
  if (empty($this->options['fields'])) {
    return;
  }
  $entity_type = $this->index
    ->getEntityType();
  $entity_info = entity_get_info($entity_type);
  foreach ($items as &$item) {
    $id = entity_id($entity_type, $item);
    $entity = current(entity_load($entity_type, [
      $id,
    ]));
    $bundle = $entity->{$entity_info['entity keys']['bundle']};
    foreach ($this->options['fields'] as $field) {
      if (isset($field['bundle'][$bundle])) {
        if ($value = token_replace($field['bundle'][$bundle], [
          $entity_type => $entity,
        ], [
          'clear' => true,
        ])) {
          $item->{$field['machine_name']} = $value;
        }
      }
    }
  }
}