使用其 API 将模块的数据公开给 Views2

发布于 2024-08-19 16:13:42 字数 5800 浏览 6 评论 0 原文

我正在分叉 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 数据的信息。有什么想法吗?

I'm forking the filefield_stats module to provide it with the ability of exposing data into the Views module via the API.
The filefield_stats schema is as follow:

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

Well, so I implemented the hook_views_api() in filefield_stats.module & added a filefield_stats.views.inc file in the module's root directory, here it is:

// $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()

According to the Views2 documentations this should work as a minimum, I think. But it doesn't! Also there is no error of any kind, when I come through the views UI, there's nothing about filefield_stats data. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-08-26 16:13:42

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

在您自己的模块中实现它们时,您始终将 hook 替换为您的模块名称。

I think your problem is in the function name: hook_views_data(), it should be filefield_stats_views_data().
hook_views_api() should also be filefield_stats_views_api().

You always replace hook with your module name when implementing them in your own modules.

转瞬即逝 2024-08-26 16:13:42

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

Missing field definitions in the code above and also wrong hook_views_api() implementation. A working API implementation example can be found here: http://drupalcode.org/sandbox/sepehr/1073868.git/tree/refs/heads/master:/modules/sms_panel_views

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文