控制block的可用与否,可以在管理员登录网站以后,在Blocks管理页面中,点击指定的block,设置哪些权限用户可以使用该block。 一个module可以创建多个block。
hook_block($op = 'list', $delta = 0, $edit = array())
{
if ($op == 'list') { // drupal 框架会首先使用list参数收集整个程序有哪些block。
$blocks[0]['info'] = t('User login');
// Not worth caching.
$blocks[0]['cache'] = BLOCK_NO_CACHE;
$blocks[1]['info'] = t('Navigation');
// Menu blocks can't be cached because each menu item can have
// a custom access callback. menu.inc manages its own caching.
$blocks[1]['cache'] = BLOCK_NO_CACHE;
$blocks[2]['info'] = t('Who\'s new');
return $blocks;
}
else if ($op == 'view') { // 当需要具体生成block内容的时候,框架使用view参数调用该hook。
$block = array();
switch ($delta) { // 当有多个block时,这个参数指定要渲染按个block,对应的是list里面的数组$blocks索引。
case 0:
// For usability's sake, avoid showing two login forms on one page.
if (!$user->uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {
$block['subject'] = t('User login');
$block['content'] = drupal_get_form('user_login_block');
}
return $block;
case 1:
if ($menu = menu_tree()) {
$block['subject'] = $user->uid ? check_plain($user->name) : t('Navigation');
$block['content'] = $menu;
}
return $block;
case 2:
if (user_access('access content')) {
// Retrieve a list of new users who have subsequently accessed the site successfully.
$result = db_query_range('SELECT uid, name FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', 0, variable_get('user_block_whois_new_count', 5));
while ($account = db_fetch_object($result)) {
$items[] = $account;
}
$output = theme('user_list', $items);
$block['subject'] = t('Who\'s new');
$block['content'] = $output;
}
return $block;
}
要想控制block显示在那个页面路径下可以通过 'pages' => 'node/*',
$blocks[0] = array(
'info' => t('My Profile'),
);
// A block can provide default settings. In this case we'll enable the
// block and make it visible only on the 'node/*' pages.
$blocks[1] = array(
'info' => t('Example: empty block'),
'status' => TRUE,
'weight' => 0,
'visibility' => 1,
'pages' => 'node/*',
);
return $blocks;
block内容通过$block['content']设置,可以直接在代码中写字符串形式的html,也可以调用theme()函数
$block['content'] = '<a>hello ,world </a>';
$block['content'] = theme("item_list",$items);