drupal7 學習筆記-----(持續更新中...)


最近折騰了一下drupal7,大概的了解了一下,頭昏那個腦漲啊。找網上的介紹倒是一大堆,但大多是e文的,中文的一半都是一知半解的敘述。 與痛苦和糾結中總結了一些經驗,幾記錄下來備用。具體如下:

一  安裝相關

  1 安裝drupal超時(主要是安裝中文翻譯的時候)

  方法一:
  修改php.ini文件:memory_limit = 256M (依實際情況設定)
  方法二:
  打開\sites\default\settings.php文件,在最后增加以下兩行:
  ini_set(‘memory_limit’, ’256M’); //加大php的內存 也可以在php.ini中設置
  ini_set(‘max_execution_time’, 2000); //加大頁面執行時間 php.ini中的默認值是30 (秒)

二  主題相關

  1 主題出問題后強制恢復系統默認主題

UPDATE system SET status = 0 WHERE type = 'theme'; 
UPDATE system SET status=1 WHERE type='theme' AND (name = 'seven' OR name = 'bartik'); 
TRUNCATE cache; 
TRUNCATE cache_block; 
TRUNCATE cache_bootstrap; 
TRUNCATE cache_field; 
TRUNCATE cache_filter; 
TRUNCATE cache_form; 
TRUNCATE cache_image; 
TRUNCATE cache_menu; 
TRUNCATE cache_path; 
TRUNCATE cache_page; 
TRUNCATE cache_update; 
TRUNCATE cache_views; 
TRUNCATE cache_views_data;

  2 獲取當前頁的模版文件列表

  在你使用的當前主題文件夾下有個template.php,在其中的xxxxx_process_page方法代碼塊中(沒有就建立)增加一下代碼:

var_dump($variables['theme_hook_suggestions']);

  這樣你隨便打開某個頁面就會在頁首打印出該頁獲取的模版文件的順序以及模版文件名稱,一目了然,注意系統加載的優先級順序是倒序的。而不用去翻n多文檔,實驗n次,糾結n次了。。。,明眼人可能看出來了,變量列表中的theme_hook_suggestions就是模版信息數組。你其實可以隨便修改定制的。。。呃,慎重,最好按照后面一個小結的方法來增加。

  3 自定義模板文件名稱

  有時候你想根據特定的格式來獲取模版文件。舉個例子,如果你想根據內容類型來制定模版,那么你可以同樣在template.php中xxxxx_process_page方法中增加一下代碼

 //增加模版選擇器
if (!empty($variables['node'])) {
    $node = $variables['node'];
    $variables['theme_hook_suggestions'][] = 'page__type__' . $node->type;
}

  這樣當drupal打開某內容的頁面時,將優先使用你定義的模版文件,是不是貌似很吊?當然更改之后記得清空系統自己的緩存,要不然它依然會加載之前默認的模板,只需執行一下:

delete from cache

  

  另外值得一提的是HOOK的復寫,上面的xxxxx_process_page其實就是 hook_process_HOOK的復寫,而drupal加載這些HOOK的順序如下:

     template_preprocess()
        template_preprocess_hello()
        helloModule_preprocess()
        helloModule_preprocess_hello()
        phptemplate_preprocess()
        phptemplate_preprocess_hello()
        helloTheme_preprocess()
        helloTheme_preprocess_hello()
        template_process()
  所以你可以自己斟酌在哪里復寫,當然你也可以集中管理,例如我的項目中我是這么寫的:
function diantang_process(&$variables,$hook){
  $node = $variables['node'];
  switch ($hook) {
    case 'comment':
      $variables['theme_hook_suggestions'][]='comment__'.$node->type;
      break;
    case 'page':
      if (!empty($variables['node'])){
        $variables['theme_hook_suggestions'][] = 'page__type__' . $node->type;
      }
    break;
    default:
      break;
  }
}

 

  4 動態改變當前頁面所對應的菜單項

  依然是在xxxxx_process_page中增加以下代碼:

if (!empty($variables['node'])) {
    $node = $variables['node'];
    //所有的 'selftype' 類型的node的menu設置成為其對應的tag的菜單項
    switch ($variables['node']->type) {
      case 'selftype':
        $tid=$variables['node']->field_tags_news['und'][0]['tid'];
        menu_set_active_item('taxonomy/term/'.$tid);
      break;
    }
  }

  這樣,如果當前頁面的內容類型是selftype的話,那么當前激活的菜單就將是該內容類型對應的標簽。其中menu_set_active_item中設置的是 菜單項的系統地址。

三 一些常用的方法

  1 獲取,node中自定義的字段
  $node= node_load(12);
  $items = field_get_items('node', $node, 'field_image');
  2 獲取node中自定義字段的可以render顯示的數組:
  $node= node_load(12);
  $items = field_get_items('node', $node, 'field_image');
  $output = field_view_value('node', $node, 'field_image', $items[0]);
  print render($output);
  3 獲取node中某文件字段(圖片,媒體)的地址
  $node= node_load(12);
  $items = field_get_items('node', $node, 'field_image');
  $url= file_create_url($items[0]["uri"])
  4 drupal 中現在已經內置了jquery和jqueryui模塊,但是jquery至增加了core,比如我們要加一個tabs;
  drupal_add_library('system', 'ui.tabs');
  drupal_add_js('jQuery(document).ready(function(){jQuery( "#mytabs" ).tabs();});', 'inline');

 

  5 drupal中常用的模塊api
  // node
  node_load($nid = NULL, $vid = NULL, $reset = FALSE);
  node_load_multiple($nids = array(), $conditions = array(), $reset = FALSE);

 

  // user
  user_load($uid, $reset = FALSE);
  user_load_multiple($uids = array(), $conditions = array(), $reset = FALSE);

 

  // menu tree
  menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL);
  menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE);

 

  // term
  taxonomy_term_load($tid) : object
  taxonomy_term_load_multiple($tids = array(), $conditions = array()) : array
  taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE) : array

 

  // block
  block_load($module, $delta);

四  一些數據庫操作

       // select
    db_select('node', 'n')
        ->extend('PagerDefault')->limit(5)
        ->fields('n');
    $statement->fetchField();
    
    db_query_range('SELECT n.nid, n.title, n.created
      FROM {node} n WHERE n.uid = :uid', 0, 10, array(':uid' => $uid));

    // insert
    $fields = array('nid' => 1, 'title' => 'my title', 'body' => 'my body');
    db_insert('node')->fields($fields)->execute();

    // update
    db_update('example')
      ->condition('id', $id)
      ->fields(array('field2' => 10))
      ->execute();

    // select
    $query = db_select('comment', 'c')
      ->fields('c', array('subject', 'name'))
      ->fields('n', array('title'))
      ->extend('PagerDefault')->limit(5)
      ->condition('n.type', array('article'), 'IN')
      ->orderBy('c.cid', 'DESC');
    $query->join('node', 'n', 'n.nid = c.nid');
    $statement = $query->execute();

    $query = db_select('node', 'n')->fields('n', array('title'))->distinct();
    $query->join('taxonomy_index', 't', 't.nid = n.nid');
    $or = db_or()->condition('n.uid', $authorId)->condition('t.tid', $cats, 'IN');
    $query->condition($or)->execute();

    // fetch
    foreach ($query->execute() as $object) {
      echo $object->name;
    }    

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM