You are here

创建一个drupal区块(6)

 

下面是完整的函数,我们的新区块如图9-7所示:
 
function approval_block($op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
        case 'list':
            $blocks[0]['info'] = t('Pending comments');
            $blocks[0]['cache'] = BLOCK_NO_CACHE;
 
            $blocks[1]['info'] = t('Unpublished nodes');
            $blocks[1]['cache'] = BLOCK_NO_CACHE;
            return $blocks;
 
        case 'configure':
            // Only in block 0 (the Pending comments block) can one
            // set the number of comments to display.
            $form = array();
            if ($delta == 0) {
                $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':
            if ($delta == 0) {
                variable_set('approval_block_num_posts', (int)
                    $edit['approval_block_num_posts']);
            }
            break;
 
        case 'view':
            if ($delta == 0 && 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);
 
                $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);
            }
            elseif ($delta == 1 && user_access('administer nodes')) {
               // Query the database for the 5 most recent unpublished nodes.
               // Unpublished nodes have their status column set to 0.
               $result = db_query_range('SELECT title, nid FROM {node} WHERE
               status = 0 ORDER BY changed DESC', 0, 5);
               $destination = drupal_get_destination();
               while ($node = db_fetch_object($result)) {
                   $items[] = l($node->title, 'node/'. $node->nid). ' '.
                      l(t('[edit]'), 'node/'. $node->nid .'/edit',
                      array('query' => $destination));
               }
 
               $block['subject'] = t('Unpublished nodes');
               // We theme our array of links as an unordered list.
               $block['content'] = theme('item_list', $items);
               }
            return $block;
    }
}
 
       由于你有多个区块,在view操作下,你使用了if…elseif构造体。在每一种情况下,你检查被查看区块的$delta以决定你是否应该运行该段代码。在脚本形式里,它看起来是这样的:
if ($delta == 0) {
// Do something to block 0
}
elseif ($delta == 1) {
// Do something to block 1
}
elseif ($delta == 2) {
// Do something to block 2
}
 
    在“未发布节点”区块启用后,区块的最终结果如图9-7所示。
9-7一个区块,列出了未发布节点

老葛的Drupal培训班 Think in Drupal

Drupal版本: