继续Drupal分享精神 QQ群:107748121 站长QQ:2275288328 skype: hellodrupal【drupal交流+drupal 建站+theme制作】
登录 注册

Drupal hook_block 区块开发实践,如何来定义一个区块显示信息。

今天基本算把网站建立完成了,比如我想要在首页显示最新新闻,我没有去用VIEW模块。想自己来开发一个区块,然后再里面显示最新的新闻动态,还有的就是在后台设置要显示新闻信息的条数。下面做了以下开发学习笔记。 '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 .= "
  • nid.">".$ff->title."
  • "; //$output.=$ff->title; } return $output; } ?> 这里主要是在区块显示里面控制自己想要显示的内容,主要是靠相关函数来实现了。 这样随心所欲,可以做比较的CMS了! 写到这里,网站基本可以完成了,下面就是主题的相关制作,只是简单设计了一点。 下面是程序效果图