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;
}
然后即可设置区块显示,最近一星期发表的内容