Drupal——module開發實現簡單block

sites\all\modules\test.info


name = "Test"
description = "exampal for test"
core = "7.x"
package = "Exampal"



sites\all\modules\test.module


<?php  
/**   
* Implements hook_help.   
*   
* Displays help and module information.  
*   
* @param path    
*   Which path of the site we're using to display help  
* @param arg    
*   Array that holds the current path as returned from arg() function  
*/  

function test_help($path, $arg) {    
	switch ($path) {      
		case "admin/help#test":        return '<p>'.  t("Displays links to nodes created on this date") .'</p>';       
		break;   
	} 
}
/**
 * Implements hook_block_info().
 */
function test_block_info() {//貌似這個就可以顯示在區塊裏了,然後再改位置
	$blocks['test'] = array(
		'info'		=> t('test'),
		'cache'		=> DRUPAL_CACHE_PER_ROLE,
	);
	return $blocks;
}
function test_contents($display='block')  
{  
    $today = getdate();  
    $start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);  
    $end_time = time();

    $max_num = variable_get('test_max', 3);

    $query = db_select('node', 'n')  
        ->fields('n', array('nid', 'title', 'created'))  
        ->condition('status', 1) //Published.  
        ->condition('created', array($start_time, $end_time), 'BETWEEN')  
        ->orderBy('created', 'DESC') //Most recent first.
        ->range(0, $max_num);
    return $query->execute();
}  	

function test_block_view($delta = '') {
	switch ($delta) {
		case 'test':
			$block['subject'] = t('test block view');
			if (user_access('access content')) {
				$result = test_contents();
				$items =array();
				foreach ($result as $node) {
					$items[] = array(
						'data' => l($node->title, 'node/' . $node->nid),
					);
				}
				if (empty($items)) {
					$block['content'] = t('No posts available');
				} else {
					$block['content'] = theme('item_list', array('items' => $items));
				}
			}
	}
	return $block;
}

然後即可設置區塊顯示,最近一星期發表的內容



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章