在 Drupal5 模組中產生一個基本列表的範例

edited 十月 2013 in Drupal
function xxx_perm() {
    return array('administer music');
}

function xxx_menu($may_cache) {
    if (!$may_cache && arg(0) == 'music' && is_numeric(arg(2))) {
        $items[] = array(
        	'path' => 'music/manage/' . arg(2),
        	'access' => true,
        	'title' => '管理歌曲',
        	'callback' => 'xxx_manage',
        	'callback arguments' => array(arg(2)),
            'type' => MENU_CALLBACK,
        );
    }
    return $items;
}

function xxx_manage($uid) {
    global $user;
    if($user->uid != $uid && !user_access('administer music')) {
        drupal_set_message('請依照網頁指示操作!');
        drupal_goto('<front>');
    } else {
        $header = array(
            array('data' => '上傳時間',   'field' => 'created'),
            array('data' => '歌曲名稱',   'field' => 'title'),
            array('data' => '歌曲或樂團名稱',   'field' => 'creator'),
            array('data' => '操作'),
        );
        $sql = 'SELECT id, created, title, creator FROM {xxx}
        WHERE uid = ' . $uid . tablesort_sql($header);
        $sql_count = 'SELECT COUNT(id) FROM {xxx} WHERE uid = ' . $uid;
        $result = pager_query($sql, 20,  0, $sql_count);
        while ($node = db_fetch_object($result)) {
            $rows[] = array(
                array('data' => $node->created),
                array('data' => $node->title),
                array('data' => $node->creator),
                array('data' => l('刪除', 'music/delete/' . $node->id)),
            );
        }
        if (!$rows) {
            $rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
        }
        $pager = theme('pager', NULL, 20, 0);
        if (!empty($pager)) {
            $rows[] = array(array('data' => $pager, 'colspan' => '4'));
        }
        return theme('table', $header, $rows, array('width' => '100%'));
    }
}

1. xxx_perm 用來產生一個 ACL 項目
2. xxx_menu 用來對應網址
3. xxx_manage 產生實際列表

原則上應該適用 Drupal6 ,不過我是在 Drupal5 開發的 ;)

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4095
Sign In or Register to comment.