2016-02-16 144 views
0

有一個需要修改的drupal站點。它已被其他人用一些自定義代碼設置和編程。他們爲每個用戶分配一些分類術語(在用戶編輯頁面中使用複選框菜單進行自定義) 但是當用戶搜索該站點時,他會找到每個分類術語的節點,而不僅僅是他自己的分類術語...僅顯示Drupal中具有特定分類術語的節點

有沒有一種方法可以對搜索結果進行過濾,將搜索結果限制爲僅具有此用戶具有這些分類標識的節點的輸出?

回答

0

我自己找到了解決方案,希望我可以幫助其他人。 我用hook_query_alter()做了它,而不是試圖獲得一個新的過濾器。

function myownmodulename_query_alter(&$query) { 

    //check if it is a search query, so we look for a table called seach_index 
    $is_search = FALSE; 
    foreach ($query->getTables() as $table) { 
     if ($table['table'] == 'search_index') { 
     $is_search = TRUE; 
     } 
    } 

    if ($is_search) { 

      // in my case: I want all the currently available taxonomy id's that have a vid=4 
      $selection_of_tids = array('SELECT * FROM taxonomy_term_data WHERE vid = 4')->fetchCol('tid');   

      // Join some relevant tables if needed - in my case I needed these 2: 
      // join table nodes and taxonomy_index on their node id "nid" column 
      $query->join('taxonomy_index', 'ti', 'n.nid = ti.nid'); 
      // join that new joined table again with the taxonomy_term_data table on their taxonomy id "tid" column 
      $query->join('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid'); 

      // add your new AND condition to the existing query 
      // so i want nodes with particular tid's ($selection_of_tids that again have vid=4) 
      $query->condition('ti.tid', $selection_of_tids, 'IN'); 

      //for an extra AND condition (x OR y) use or add it this way: 
      $query->condition(db_or()->condition('ti.tid', 1, '=')->condition('ti.tid', 2, '=')); 

      // for testing purposes: use "print" query instead of "var_dump" 
      // and you can always check the search query in your website on top 
      echo "<pre>"; print $query; echo "</pre>"; 

    } 

} 
相關問題