You are here

Drupal专业开发指南 第17章 构建一个jQuery投票小部件(Widget)

g089h515r806 的头像
Submitted by g089h515r806 on 星期一, 2009-08-24 15:34

老葛的Drupal培训班 Think in Drupal

让我们编写一个稍微复杂一点的启用jQuery的Drupal模块。我们将构建一个AJAX投票小部件,如图17-7所示,它可以让用户为他们喜欢的文章加一分。我们将使用jQuery来进行投票和修改总的投票分数,而不用重新加载整个页面。我们还添加一个基于角色的权限,这样只有具有“rate content”权限的用户才允许投票。由于用户的每次投票只能增加一分,所以让我们将模块命名为“plusone”。
 
17-7.投票小部件
 
    在我们开始为plusone模块编写实际的jQuery部分以前,首先需要构建模块所需的一些基本代码。如果你以前从来没有构建过模块,请参看第2章。否则,那么就让我们现在开始吧!
    在sites/all/modules/custom中创建一个名为plusone的目录(你可能首先需要创建sites/all/modules/custom目录)。在plusone目录中,创建文件plusone.info,向里面添加以下内容:
 
; $Id$
name = Plus One
description = "A +1 voting widget for nodes. "
package = Pro Drupal Development
core = 6.x
 
    这个文件将该模块注册到了Drupal中,这样可以通过管理界面启用或者禁用模块了。
    接着,我们将创建plusone.install文件。当启用、禁用、安装、卸载这个模块时,就会调用这个PHP文件中的函数;它一般用来创建或者删除数据库表。在这里,我们想用来追踪谁在哪个节点上投了票:
 
<?php
// $Id$
 
/**
 * Implementation of hook_install().
 */
function plusone_install() {
    // Create tables.
    drupal_install_schema('plusone');
}
 
/**
 * Implementation of hook_uninstall().
 */
function plusone_uninstall() {
    // Remove tables.
    drupal_uninstall_schema('plusone');
}
 
/**
 * Implementation of hook_schema().
 */
function plusone_schema() {
    $schema['plusone_votes'] = array(
        'description' => t('Stores votes from the plusone module.'),
        'fields' => array(
            'uid' => array(
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
                'description' => t('The {user}.uid of the user casting the vote.'),
            ),
            'nid' => array(
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
                'description' => t('The {node}.nid of the node being voted on.'),
            ),
            'vote_count' => array(
                'type' => 'int',
                'not null' => TRUE,
                'default' => 0,
                'description' => t('The number of votes cast.'),
            ),
        ),
        'primary key' => array('uid', 'nid'),
        'indexes' => array(
            'nid' => array('nid'),
            'uid' => array('uid'),
        ),
    );
    return $schema;
}
 
    还有,添加文件sites/all/modules/custom/plusone/plusone.css。这个文件不是必需的,但它可以使投票小部件的外观更漂亮一些,如图17-8所示。
 
17-8. 对比带有CSS投票小部件和不带有CSS的投票小部件
 
    向plusone.css添加以下内容:
 
div.plusone-widget {
    width: 100px;
    margin-bottom: 5px;
    text-align: center;
}
div.plusone-widget .score {
    padding: 10px;
    border: 1px solid #999;
    background-color: #eee;
    font-size: 175%;
}
div.plusone-widget .vote {
    padding: 1px 5px;
    margin-top: 2px;
    border: 1px solid #666;
    background-color: #ddd;
}
 
    我们已经创建了外围支持文件,现在让我们关注模块文件本身和jQuery JavaScript文件。创建两个空文件,sites/all/modules/custom/plusone/plusone.js和sites/all/modules/custom/plusone/plusone.module,在接下来的几步中,我们将逐步的向这两个文件添加代码。总结一下,我们已经创建了以下文件:
 
sites/
    all/
        modules/
            custom/
                plusone/
                    plusone.js
                    plusone.css
                    plusone.info
                    plusone.install
                    plusone.module

Drupal版本: