在Drupal中仅显示具有特定分类术语的节点

时间:2016-02-16 11:51:59

标签: javascript html string wordpress replace

我需要修改一个drupal网站。它已由其他人设置和编程,带有一些自定义代码。他们为每个用户分配了一些分类术语(使用用户编辑页面中的复选框菜单自定义) 但是当用户搜索网站时,他会找到每个分类术语的节点,而不仅仅是他自己的分类术语......

有没有办法可以在搜索结果上设置过滤器,将输出限制为只有具有这个用户拥有的分类标识的节点?

1 个答案:

答案 0 :(得分: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>";

    }

}