Drupal 7 Search API钩子注册的回调类未触发

时间:2019-03-12 14:23:28

标签: php drupal drupal-7

我对PHP(除了很多年前的一些小尝试)和Drupal 7还是很陌生。我正在寻找一个使用Search API的项目,并尝试从搜索索引中排除一些文件。为此,我遵循了link

但是它不起作用。更改选项未显示在搜索API文件索引的配置中。我添加了以下两个文件:

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

该钩子被激活,我可以从在search_api回调数组中注册的第一个文件中找到回调。但是,永远不会解决注册的类SearchApiExcludeWebformFiles。其他内置更改(我尝试了几种用户特定的更改,例如用户内容)正在起作用,这意味着,如果我只是在supportsIndex函数中返回true,则可以在任何搜索API索引中激活它们。如您所见,我也只是在此返回true,但仍然没有在我的任何搜索API索引中显示它。

我是否错过了有关SearchApiExcludeWebformFiles类正确注册的事情?还是有其他因素阻止此设置成功?

谢谢。

1 个答案:

答案 0 :(得分:0)

在相互更改了许多组件和模块部分之后,我得出以下结论:Drupal除了.module文件之外,不喜欢其他文件。我将整个类重新插入到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['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]);
            }
        }
    }
}

我还更改了数组名和类名,但是位置是对我有用的最后一步。