Block 操作

1 篇文章 / 0 new
author
Block 操作
主要 hook
hook_block_info() 定義區塊
visibility 參數為 BLOCK_VISIBILITY_PHP時, 除搭配 pages 參數外, 另 PHP filter 模組也必須啟用
block.api.php
hook_block_configure($delta = '') 提供給系統UI, 區塊參數設定項目  
hook_block_save($delta = '', $edit = array()) 儲存參數資料  
hook_block_view($delta = '') 區塊內容的呈現  
相關資料庫
// Implements hook_block_info().
function approval_block_info() {
    $blocks['pending_comments'] = array( //block key
        'info' => t('Pending Comments'),
        'status' => TRUE,
        'region' => 'sidebar_first',
        'weight' => 0,
        //'visibility' => BLOCK_VISIBILITY_LISTED,//1:僅列出的頁面, BLOCK_VISIBILITY_NOTLISTED:(default)除列出之外的所有頁面
        //BLOCK_VISIBILITY_PHP:由程式決定,傳回true則顯示 ,搭配 pages 參數, 另 PHP filter 模組必須啟用
        //'pages' => '< ?php return false; ? >',
    );
    $blocks['unpublished_nodes'] = array(
        'info' => t('Unpublished Nodes'),
        'status' => FALSE,
        'region' => 'sidebar_first',
        'weight' => 0,
    );
    return $blocks;
}
// Implements hook_block_configure().  設定參數資料
function approval_block_configure($delta) {
    $form = array();
    switch ($delta) { //block key
        case 'pending_comments':
            $form['pending_comment_count'] = array(
                '#type' => 'textfield',
                '#title' => t('Configure Number of Comments to Display'),
                '#size' => 6,
                '#description' => t('Enter the number of pending comments that will appear in the block.'),
                '#default_value' => variable_get('pending_comment_count', 5),
            );
            break;
        case 'unpublished_nodes':
            $form['unpublished_node_count'] = array(
                '#type' => 'textfield',
                '#title' => t('Configure Number of Nodes to Display'),
                '#size' => 6,
                '#description' => t('Enter the number of unpublished nodes that will appear in the block.'),
                '#default_value' => variable_get('unpublished_node_count', 5),
            );
            break;
    }
    return $form;
}
// Implements hook_block_save().  儲存參數值
function approval_block_save($delta = '', $edit = array()) {
    switch ($delta) { //action block key
        case 'pending_comments':
            variable_set('pending_comment_count', (int) $edit['pending_comment_count']);
            break;
        case 'unpublished_nodes':
            variable_set('unpublished_node_count', (int) $edit['unpublished_node_count']);
            break;
    }
    return;
}
//Implements hook_block_view().  * 區塊內容呈現
function approval_block_view($delta = '') {
    switch ($delta) {//action block key
        case 'pending_comments':
            $block['subject'] = t('Pending Comments'); //標題
            $block['content'] = approval_block_contents($delta); //內容
            return $block;
            break;
        case 'unpublished_nodes':
            $block['subject'] = t('Unpublished Nodes');
            $block['content'] = approval_block_contents($delta);
            return $block;
            break;
    }
}
//A module-defined block content function.
function approval_block_contents($delta) {
    switch ($delta) {
        case 'pending_comments':
            if (user_access('administer comments')) {
                $nbr_comments = variable_get('pending_comment_count',5);
                $result = db_query("SELECT cid, subject FROM {comment} WHERE status = 0 limit $nbr_comments");
                $items = array();
                foreach ($result as $row) {
                    $items[] = l($row->subject, 'comment/' . $row->cid . '/edit');
                }
                return array('#markup' => theme('item_list', array('items' => $items)));
            }
            break;
        case 'unpublished_nodes':
            if (user_access('administer nodes')) {
                $nbr_nodes = variable_get('unpublished_node_count',5);
                $result = db_query("SELECT nid, title FROM {node} WHERE status = 0 limit $nbr_nodes");
                $items = array();
                foreach ($result as $row) {
                    $items[] = l($row->title, 'node/' . $row->nid . '/edit');
                }
                return array('#markup' => theme('item_list', array('items' => $items)));
            }
            break;
    }
}
Free Web Hosting