0
votes

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.

1

1 Answers

0
votes

After inter changing many of the components and module parts, I came to the following conclusion: Drupal doesn't like extra files besides the .module ones. I reinserted my whole class into the search_exclude_webform_files.module file. This way, the class could be found. My module file is now looking like this:

<?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['search_api_exclude_webform_files'] = array(
        'name' => t('Exclude private webform files'),
        'description' => t('Excludes private webform files from being indexed in search'),
        'class' => 'SearchApiAlterExcludeFormFiles',
    );
    return $callbacks;
}

/**
 * The following class is used to provide file filtering for webform files. It ensures
 * they are not indexed by Search.
 */
class SearchApiAlterExcludeFormFiles extends SearchApiAbstractAlterCallback {


    // This filter is only available on file entities
    public function supportsIndex(SearchApiIndex $index) {
        return $index->getEntityType() === 'file';
    }


    // 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]);
            }
        }
    }
}

I also changed the array name and class name, but the location was the final step that did the trick for me.