You are here

对非节点的内容进行索引:hook_update_index()(1)

g089h515r806 的头像
Submitted by g089h515r806 on 星期四, 2009-08-20 15:00

在你需要对非Drupal节点的内容进行搜索时,那么你可以钩住索引器并向其提供你需要的任何文本数据,这样它们在Drupal中就可被搜索了。假定你的小组支持一个遗留应用系统,这个系统可用来输入和查看最近几年的产品技术笔记。由于一些政策原因,你还不能完全使用Drupal的解决方案来替代这个遗留系统,但是你想在Drupal内部能够搜索这些技术笔记。没问题。让我们假定遗留系统将它的数据保存在了technote表中。我们将创建一个简短的模块,在里面使用hook_update_index()把这个数据库中的信息发送给Drupal的索引器,使用hook_search()将搜索结果显示出来。

 
注意 如果你想对非Drupal的数据库的内容进行索引,那么就需要连接多个数据库,关于这方面的更多详细,可参看第5章。
 
    在sites/all/modules/custom下面创建一个名为legacysearch的文件夹。由于我们需要一个用来测试的遗留数据库,所以创建一个名为legacysearch.install的文件,并添加以下内容:
 
<?php
// $Id$
 
/**
 * Implementation of hook_install().
 */
function legacysearch_install() {
    // Create table.
    drupal_install_schema('legacysearch');
    // Insert some data.
    db_query("INSERT INTO technote VALUES (1, 'Web 1.0 Emulator',
        '<p>This handy product lets you emulate the blink tag but in
        hardware...a perfect gift.</p>', 1172542517)");
    db_query("INSERT INTO technote VALUES (2, 'Squishy Debugger',
        '<p>Fully functional debugger inside a squishy gel case.
        The embedded ARM processor heats up...</p>', 1172502517)");
}
 
/**
 * Implementation of hook_uninstall().
 */
function legacysearch_uninstall() {
    drupal_uninstall_schema('legacysearch');
}
 
/**
 * Implementation of hook_schema().
 */
function legacysearch_schema() {
    $schema['technote'] = array(
        'description' => t('A database with some example records.'),
        'fields' => array(
            'id' => array(
                'type' => 'serial',
                'not null' => TRUE,
                'description' => t("The tech note's primary ID."),
            ),
            'title' => array(
                'type' => 'varchar',
                'length' => 255,
                'description' => t("The tech note's title."),
            ),
            'note' => array(
                'type' => 'text',
                'description' => t('Actual text of tech note.'),
            ),
            'last_modified' => array(
                'type' => 'int',
                'unsigned' => TRUE,
                'description' => t('Unix timestamp of last modification.'),
            ),
        ),
        'primary key' => array('id'),
    );
    return $schema;
}

老葛的Drupal培训班 Think in Drupal

Drupal版本:

评论

biglazy 的头像

官方勘误:http://www.drupalbook.com/errata2?page=6    Page 304

Error: 

db_query("INSERT INTO technote VALUES ...

Correction: 

db_query("INSERT INTO {technote} VALUES ...

Description of the Error: 

You have to include '{' and '}' brackets, so the database engine can add table prefix (if needed) to the table name