关闭

Drupal——module开发实现简单block

标签: drupal
724人阅读 评论(0) 收藏 举报
分类:

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;
}

然后即可设置区块显示,最近一星期发表的内容



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:31324次
    • 积分:575
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:71篇
    • 译文:1篇
    • 评论:0条
    文章分类