You are here

创建一个drupal区块(4)

 

通过使用Drupal自带的变量系统的函数variable_set(),你将区块中显示的待定评论的数目保存了下来。注意这里使用了类型转换,将其转换为了整数,目的是对数据进行明智检查。最后添加view操作,当区块显示时,返回一个待定评论列表。
 
function approval_block($op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
        case 'list':
            $blocks[0]['info'] = t('Pending comments');
            return $blocks;
 
        case 'configure':
            $form['approval_block_num_posts'] = array(
                '#type' => 'textfield',
                '#title' => t('Number of pending comments to display'),
                '#default_value' => variable_get('approval_block_num_posts', 5),
            );
            return $form;
 
        case 'save':
            variable_set('approval_block_num_posts',
                (int)$edit['approval_block_num_posts']);
            break;
 
        case 'view':
            if (user_access('administer comments')) {
               // Retrieve the number of pending comments to display that
               // we saved earlier in the 'save' op, defaulting to 5.
               $num_posts = variable_get('approval_block_num_posts', 5);
 
               // Query the database for unpublished comments.
               $result = db_query_range('SELECT c.* FROM {comments} c WHERE
               c.status = %d ORDER BY c.timestamp', COMMENT_NOT_PUBLISHED, 0,
               $num_posts);
 
               // Preserve our current location so user can return after editing.
               $destination = drupal_get_destination();
               $items = array();
               while ($comment = db_fetch_object($result)) {
                   $items[] = l($comment->subject, 'node/'. $comment->nid,
                      array('fragment' => 'comment-'. $comment->cid)) .' '.
                      l(t('[edit]'), 'comment/edit/'. $comment->cid,
                      array('query' => $destination));
               }
 
               $block['subject'] = t('Pending comments');
               // We theme our array of links as an unordered list.
               $block['content'] = theme('item_list', $items);
            }
            return $block;
    }
}

老葛的Drupal培训班 Think in Drupal

Drupal版本:

评论

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

Error: 

The following line of code:
$blocks[0]['cache'] = BLOCK_NO_CACHE;

is missing from:

case 'list':
$blocks[0]['info'] = t('Pending comments');
return $blocks;

Correction: 

case 'list':
$blocks[0]['info'] = t('Pending comments');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;

Description of the Error: 

The block cache setting is missing from the approval.module code example on page 213. Its is included in all other examples of approval.module code in the "Building a Block" section.