Custom Search API index condition in Drupal 7
/**
* Implements hook_search_api_alter_callback_info().
* See includes/callback_search_api_people_archive.inc for scope.
*/
function HOOK_search_api_alter_callback_info() {
$callbacks['people_archive_alter'] = array(
'name' => t('People archive'),
'description' => t('People archive.'),
'class' => 'SearchApiPeopleArchiveAlter',
'weight' => 100,
);
return $callbacks;
}
And the class itself:
/**
* Search API People archive data alteration callback that will index only
* node Title if People node is archived.
*/
class SearchApiPeopleArchiveAlter extends SearchApiAbstractAlterCallback {
public function supportsIndex(SearchApiIndex $index) {
return $index->getEntityType() === 'node';
}
public function alterItems(array &$items) {
if (!$items)
return;
foreach ($items as &$item) {
$archive_people_field = field_get_items('node', $item, 'field_archive');
// For archived People we need to index only Node title.
if (isset($archive_people_field[0]['value']) && $archive_people_field[0]['value']) {
$item = $this->peopleArchiveUnsetFields($item);
}
}
}
private function peopleArchiveUnsetFields(&$item) {
// Unneeded fields,
// tip: $this->index->getFields()
$excludeIndexFields = array(
'body',
);
foreach ($item as $field_name => $field_data) {
if (in_array($field_name, $excludeIndexFields) && is_array($field_data)) {
$item->{$field_name} = array();
}
}
return $item;
}
}