欢迎各位兄弟 发布技术文章
这里的技术是共享的
Template:Drupal top 本条目主要内容:比如想要在首页显示最新新闻,不去用VIEW模块。而是自己来开发一个区块,然后在里面显示最新的新闻动态,还有的就是在后台设置要显示新闻信息的条数。下面做了以下开发笔记。
<?php
/**
* 区块钩子实现
*/
function product_block($op = 'list', $delta = 0,$edit = array()) {
global $user;
switch($op){
case 'list':// 下面我定义了四个区块列表,这里主要是为首页显示信息定义的,定义了以后会在后台显示你帝国一的区块信息,不信你可以试试
$blocks[0]['info'] = t('最新公司动态');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
$blocks[1]['info'] = t('公司公告区块');
$blocks[1]['cache'] = BLOCK_NO_CACHE;
$blocks[2]['info'] = t('推荐产品');
$blocks[2]['cache'] = BLOCK_NO_CACHE;
$blocks[3]['info'] =t('图片新闻');
$blocks[3]['cache']= BLOCK_NO_CACHE;
return $blocks;
case 'configure': // 这里就是定义区块的一些属性,比如新闻显示条数等,
$form = array();
if($delta == 0){ //如果是'最新公司动态' 那我来设置一下要显示的新闻条数
$form['product_block_news_count'] = array(
'#type' => 'select',
'#title' => t('最大数目'),
'#default_value' =>variable_get('product_block_news_count',6),// variable_get 来得到这个变量的值
'#options' => drupal_map_assoc(array(2, 4, 6, 8, 10, 16, 20, 24, 28, 30)),
);
}
else if($delta == 1){
$form['product_block_note_content'] = array(
'#type' => 'textarea',
'#title' => t('信息内容'),
'#default_value' =>variable_get('product_block_note_content',6),
);
}
else if($delta == 2){
$form['product_block_top_new_product'] = array(
'#type' => 'textfield',
'#title' => t('展示数量'),
'#default_value' => variable_get('product_block_top_new_product',6),
);
}
else if($delta == 3){
$form['product_block_images_news_count'] = array( // $from['表单变量'] 要和 variable_get['变量'] 一样 因为它就是get from 表单的一个名称
'#type' => 'select',
'#title' => t('图片新闻数量'),
'#default_value' => variable_get('product_block_images_news_count',2),
'#options' => drupal_map_assoc(array(2,3,4,6,8,10,12,14,16,18)),
);
}
return $form;
case 'save':// 这里主要是保存区块变量信息的操作 就等于是save事件,然后会激活下面的操作
if($delta == 0){
//注意这里是保持数据,用的是_set
variable_set('product_block_news_count',$edit['product_block_news_count']);
}
else if($delta == 1){
variable_set('product_block_note_content',$edit['product_block_note_content']);
}
else if($delta == 2){
variable_set('product_block_top_new_product',$edit['product_block_top_new_product']);
}
else if($delta ==3){
variable_set('product_block_images_news_count',$edit['product_block_images_news_count']);
}
break;
case 'view':// 这就是VIEW事件操作了。
if($delta == 0 && user_access('access content')){
$max_news_count = variable_get('product_block_news_count',1); //得到一个最新闻变量的值
$result = db_query_range(db_rewrite_sql
("SELECT n.nid, n.title, n.sticky,n.created FROM {node} n WHERE n.type =
'story' AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC"), 0, $max_news_count);
if ($node_title_list = pnode_title_list($result)) {
$block['content'] = $node_title_list; // 给区块内容赋值
$block['content'] .= theme('more_link', url('taxonomy/term/8+9'), t('Read the latest story entries.')); //加上一个更多衔接
$block['subject'] = t('最新公司动态'); // 区块内容显示的标题
return $block;
}
}
else if(
$delta == 1 && user_access('access content')){
$block['subject'] = t('公司公告');
$block['content'] =variable_get('product_block_note_content',0);// 我把公告直接作为变量保持在变量表里了
return $block;
}
else if(
$delta == 2 && user_access('view product')){// 这里使用了view product 权限,如果用access_content 权限不起作用 因为前面product_access 已经设置了一个view,
所以不会继承上一级node权限.当时我就匿名看不到信息!
$max_top_new_product = variable_get('product_block_top_new_product',6);
$block['content'] = product_loadfiles($max_top_new_product,'product'); //这里主要是用于现实产品图片信息的函数调用
$block['subject'] = t('ff');
return $block;
}
else if(
$delta ==3 && user_access('access content')){
$max_images_news_count = variable_get('product_block_images_news_count',2);
$block['content'] =product_loadfiles($max_images_news_count,'story');
$block['subject'] = t('title');
return $block;
}
}
}
function product_loadfiles($count,$type) {//读取图片
$result = db_query_range(db_rewrite_sql("SELECT u.fid,f.filepath,n.title,n.nid FROM
{upload} u INNER JOIN {files} f ON u.fid = f.fid INNER JOIN {node}
n ON n.nid = u.nid WHERE n.type ='".$type."' ORDER BY n.sticky DESC, n.created DESC "), 0, $count);
while (
$ff = db_fetch_object($result)) {
$output .= "<li><a href=node/".$ff->nid."><img src=".$ff->filepath." width=80 height=60><span>".$ff->title."</span></a></li>";
//$output.=$ff->title;
}
return $output;
}
来自 http://www.zzbaike.com/wiki/Drupal/Drupal_hook_block%E5%8C%BA%E5%9D%97%E5%BC%80%E5%8F%91%E5%AE%9E%E8%B7%B5