使用API​​将模块的数据暴露给Views2

时间:2010-01-30 11:37:39

标签: php drupal drupal-6 drupal-views drupal-modules

我要求filefield_stats模块为其提供通过Views将数据公开到API模块的功能。 filefield_stats架构如下:

function filefield_stats_schema() {
  $schema['filefield_stats'] = array(
    'fields' => array(      
      'fid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Primary Key: the {files}.fid'),
      'vid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'Primary Key: the {node}.vid'),      
      'uid'       => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The {users}.uid of the downloader'),
      'timestamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'description' => 'The timestamp of the download'),
      'hostname'  => array('type' => 'varchar', 'length' => 128, 'not null' => TRUE, 'default' => '', 'description' => 'The hostname downloading the file (usually IP)'),
      'referer'   => array('type' => 'text', 'not null' => FALSE, 'description' => 'Referer for the download'),   
    ),
    'indexes' => array('fid_vid' => array('fid', 'vid')),
  );
  return $schema;
}

好吧,所以我在 filefield_stats.module &中实现了 hook_views_api()。在模块的根目录中添加了 filefield_stats.views.inc 文件,这里是:

// $Id$

/**
 * @file
 * Provide the ability of exposing data to Views2, for filefield_stats module.
 */

function filefield_stats_views_data() {
    $data = array();
    $data['filefield_stats']['table']['group'] = t('FilefieldStats');

    // Referencing the {node_revisions} table.
    $data['filefield_stats']['table']['join'] = array(
        'node_revisions' => array(
            'left_field' => 'vid',
            'field' => 'vid',
        ),
        'files' => array(
            'left_field' => 'fid',
            'field' => 'fid',
        ),
        'users' => array(
            'left_field' => 'uid',
            'field' => 'uid',
        ),
    );

    // Introducing filefield_stats table fields to Views2.
    // vid: The node's revision ID which wrapped the downloaded file
    $data['filefield_stats']['vid'] = array(
        'title' => t('Node revision ID'),
        'help' => t('The node\'s revision ID which wrapped the downloaded file'),
        'relationship' => array(
            'base' => 'node_revisions',
            'field' => 'vid',
            'handler' => 'views_handler_relationship',
            'label' => t('Node Revision Reference.'),
        ),
    );

    // uid: The ID of the user who downloaded the file.
    $data['filefield_stats']['uid'] = array(
        'title' => t('User ID'),
        'help' => t('The ID of the user who downloaded the file.'),
        'relationship' => array(
            'base' => 'users',
            'field' => 'uid',
            'handler' => 'views_handler_relationship',
            'label' => t('User Reference.'),
        ),
    );

    // fid: The ID of the downloaded file.
    $data['filefield_stats']['fid'] = array(
        'title' => t('File ID'),
        'help' => t('The ID of the downloaded file.'),
        'relationship' => array(
            'base' => 'files',
            'field' => 'fid',
            'handler' => 'views_handler_relationship',
            'label' => t('File Reference.'),
        ),
    );

    // hostname: The hostname which the file has been downloaded from.
    $data['filefield_stats']['hostname'] = array(
        'title' => t('The Hostname'),
        'help' => t('The hostname which the file has been downloaded from.'),
        'field' => array(
            'handler' => 'views_handler_field',
            'click sortable' => TRUE,
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_string',
        ),
        'argument' => array(
            'handler' => 'views_handler_argument_string',
        ),
    );

    // referer: The referer address which the file download link has been triggered from.
    $data['filefield_stats']['referer'] = array(
        'title' => t('The Referer'),
        'help' => t('The referer which the file download link has been triggered from.'),
        'field' => array(
            'handler' => 'views_handler_field',
            'click sortable' => TRUE,
        ),
        'sort' => array(
            'handler' => 'views_handler_sort',
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_string',
        ),
        'argument' => array(
            'handler' => 'views_handler_argument_string',
        ),
    );

    // timestamp: The time of the download.
    $data['filefield_stats']['timestamp'] = array(
        'title' => t('Download Time'),
        'help' => t('The time of the download.'),
        'field' => array(
            'handler' => 'views_handler_field_date',
            'click sortable' => TRUE,
        ),
        'sort' => array(
            'handler' => 'views_handler_sort_date',
        ),
        'filter' => array(
            'handler' => 'views_handler_filter_date',
        ),
    );

    return $data;
} // filefield_stats_views_data()

根据Views2文件,我认为这应该是最低限度的。但事实并非如此!此外,没有任何错误,当我通过视图UI时,没有关于filefield_stats数据的信息。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

我认为您的问题出在函数名称中:hook_views_data(),它应该是filefield_stats_views_data()hook_views_api()也应为filefield_stats_views_api()

在您自己的模块中实现时,您总是将模块名称替换为钩子。

答案 1 :(得分:2)

上述代码中缺少field个定义以及错误的hook_views_api()实施。可以在此处找到有效的API实现示例:http://drupalcode.org/sandbox/sepehr/1073868.git/tree/refs/heads/master:/modules/sms_panel_views