久久精品水蜜桃av综合天堂,久久精品丝袜高跟鞋,精品国产肉丝袜久久,国产一区二区三区色噜噜,黑人video粗暴亚裔

Drupal/Drupal hook block區(qū)塊開發(fā)實(shí)踐

來自站長(zhǎng)百科
跳轉(zhuǎn)至: 導(dǎo)航、? 搜索

模板:Drupal top 本條目主要內(nèi)容:比如想要在首頁顯示最新新聞,不去用VIEW模塊。而是自己來開發(fā)一個(gè)區(qū)塊,然后再里面顯示最新的新聞動(dòng)態(tài),還有的就是在后臺(tái)設(shè)置要顯示新聞信息的條數(shù)。下面做了以下開發(fā)筆記。

Drupal hook block區(qū)塊開發(fā)實(shí)踐[ ]

<?php
/**
* 區(qū)塊鉤子實(shí)現(xiàn)   
*/
function product_block($op = 'list', $delta = 0,$edit = array()) {
  global $user;
  switch($op){
    case 'list':// 下面我定義了四個(gè)區(qū)塊列表,這里主要是為首頁顯示信息定義的,定義了以后會(huì)在后臺(tái)顯示你帝國(guó)一的區(qū)塊信息,不信你可以試試
        $blocks[0]['info'] = t('最新公司動(dòng)態(tài)');
        $blocks[0]['cache'] = BLOCK_NO_CACHE;
       

$blocks[1]['info'] = t('公司公告區(qū)塊');
        $blocks[1]['cache'] = BLOCK_NO_CACHE;
       

$blocks[2]['info'] = t('推薦產(chǎn)品');
        $blocks[2]['cache'] = BLOCK_NO_CACHE;
       

$blocks[3]['info'] =t('圖片新聞');
        $blocks[3]['cache']= BLOCK_NO_CACHE;
        return $blocks;
    case 'configure': // 這里就是定義區(qū)塊的一些屬性,比如新聞顯示條數(shù)等,
       $form = array();
        if($delta == 0){ //如果是'最新公司動(dòng)態(tài)' 那我來設(shè)置一下要顯示的新聞條數(shù)
          $form['product_block_news_count'] = array(
            '#type' => 'select',
            '#title' => t('最大數(shù)目'),
            '#default_value' =>variable_get('product_block_news_count',6),//  variable_get 來得到這個(gè)變量的值
             '#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('信息內(nèi)容'),
             '#default_value' =>variable_get('product_block_note_content',6),
          );
        }
        else if($delta == 2){
            $form['product_block_top_new_product'] = array(
              '#type' => 'textfield',
              '#title' => t('展示數(shù)量'),
              '#default_value' => variable_get('product_block_top_new_product',6),
            );
        }
        else if($delta == 3){
            $form['product_block_images_news_count'] = array( // $from['表單變量'] 要和 variable_get['變量'] 一樣 因?yàn)樗褪莋et from 表單的一個(gè)名稱
              '#type' => 'select',
              '#title' => t('圖片新聞數(shù)量'),
              '#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':// 這里主要是保存區(qū)塊變量信息的操作 就等于是save事件,然后會(huì)激活下面的操作
        if($delta == 0){
         //注意這里是保持?jǐn)?shù)據(jù),用的是_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); //得到一個(gè)最新聞變量的值
            $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; // 給區(qū)塊內(nèi)容賦值
                    $block['content'] .= theme('more_link', url('taxonomy/term/8+9'), t('Read the latest story entries.')); //加上一個(gè)更多銜接
                    $block['subject'] = t('最新公司動(dòng)態(tài)'); // 區(qū)塊內(nèi)容顯示的標(biāo)題
                    
                    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 權(quán)限,如果用access_content 權(quán)限不起作用 因?yàn)榍懊鎝roduct_access 已經(jīng)設(shè)置了一個(gè)view,
 所以不會(huì)繼承上一級(jí)node權(quán)限.當(dāng)時(shí)我就匿名看不到信息!
         $max_top_new_product = variable_get('product_block_top_new_product',6);
                 

$block['content'] = product_loadfiles($max_top_new_product,'product'); //這里主要是用于現(xiàn)實(shí)產(chǎn)品圖片信息的函數(shù)調(diào)用
                 $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;
}

?>

這里主要是在區(qū)塊顯示里面控制自己想要顯示的內(nèi)容,主要是靠相關(guān)函數(shù)來實(shí)現(xiàn)了。

參考來源[ ]

http://hellodrupal.info/node/37

模板:Drupal