I'm fairly new to PHP (except some minor attempts many years ago) and Drupal 7 as well. I'm looking into a project using the Search API and try to exclude some files from my search index. To achieve this, I followed this link.
But it won't work. The alteration option is not showing in the configuration for the search API file index. I added the following two files:
search_exclude_webform_files.module
<?php
/*
* Implements hook_search_api_alter_callback_info()
*/
function search_exclude_webform_files_search_api_alter_callback_info() {
// Adds a filter to exclude private files from the index
$callbacks['exclude_private_files'] = array(
'name' => t('Exclude private files'),
'description' => t('Excludes private webform files from being indexed in search'),
'class' => 'SearchApiExcludeWebformFiles',
'weight' => 100,
);
return $callbacks;
}
callback_private_webform_files.inc
<?php
/**
* @file
* Contains SearchApiExcludeWebformFiles.
*/
/**
* The following class is used to provide file filtering for webform files. It ensures
* they are not indexed by Search.
*/
class SearchApiExcludeWebformFiles extends SearchApiAbstractAlterCallback {
// This filter is only available on file entities
public function supportsIndex(SearchApiIndex $index) {
// return $index->getEntityType() === 'file';
return true;
}
// For each file item that is indexed if the URI field contains the private
// prefix, do not index the file by unsetting it
public function alterItems(array &$items) {
foreach ($items as $k => $item) {
if (strpos($item->uri, 'private://webform') !== false || strpos($item->uri, 'files/private/webform') !== false || strpos($item->uri, 'files/webform') !== false) {
unset($items[$k]);
}
}
}
}
The hook activates and I can find the callback from the first file registered in the search_api callbacks array. However, the registered class SearchApiExcludeWebformFiles is never addressed. Other built-in alterations (I tried several user-specific ones, such as user-content) are working, meaning that I can activate them in any search API index if I just return true in the supportsIndex-function. As you can see, I'm just returning true in this one as well, but still, it's not showing in any of my search API indices.
Did I miss something regarding the correct registering of the class SearchApiExcludeWebformFiles? Or is there something else that's preventing this setup from succeeding?
Thanks and greetings.