/admin/config/search/apachesolr 在后台配置页面中进行过滤
1) 对表单处理
//重组数组 $arr1是含英文键中文值,$arr2只有英文键作为值
function _cz_arr($arr1,$arr2){
//$arr1 类似结果
// (Array, 10 elements)
// feed_item (Array, 3 elements)
// to_do (Array, 3 elements)
// hui_yi_jue_ding (Array, 3 elements)
// wp_blog_clone_2 (Array, 4 elements)
// wp_blog_clone_1 (Array, 4 elements)
// wp_blog (Array, 3 elements)
// page (Array, 3 elements)
// article (Array, 4 elements)
// feed (Array, 3 elements)
// question_answer (Array, 3 elements)
// $arr2 类似结果
// 0 (String, 7 characters ) article
// 1 (String, 15 characters ) wp_blog_clone_1
// 2 (String, 15 characters ) wp_blog_clone_2
$arr2= array_flip($arr2);
$arr_result = array();
foreach($arr2 as $key=>$value){
//用户具有访问权限时
if(user_access('view any '.$key.' content')){
$arr_result[$key] = $arr1[$key]['label'];
}
}
return $arr_result;
}
function custom_four_form_alter(&$form, &$form_state, $form_id)
{
global $user;
// $e = kprint_r(entity_get_info(),TRUE);
// print $e;
// var_dump(entity_get_info()['node']);
$arr1 = entity_get_info()['node']['bundles']; //得到所有内容类型
$arr2 = apachesolr_get_index_bundles('solr', 'node'); //得到要过滤的内容类型
$con_typ_arrs = _cz_arr($arr1,$arr2);
if(!empty($_GET['search_content_type'])){
$default_values = array();
foreach($arr2 as $key=>$value){
!empty($_GET[$value]) && $default_values[] = $value;
}
}
else{
$default_values = $arr2;
}
if('apachesolr_search_custom_page_search_form' == $form_id ){
$form['resource_type'] = array(
'#type' => 'checkboxes',
'#title' => '类型',
'#options' => $con_typ_arrs,
'#default_value' => $default_values
);
// $form['actions']['submit']['#submit'][]= 'custom_four_apachesolr_search_custom_page_search_form_submit';
$form['#submit'][]= 'custom_four_apachesolr_search_custom_page_search_form_submit';
}
// $e = kprint_r($form,TRUE);
// print $e;
}
//主要是覆写人家原来的 apachesolr_search_custom_page_search_form_submit 代码,进行了些许修改,见下面红包的字
function custom_four_apachesolr_search_custom_page_search_form_submit($form, &$form_state) {
$search_page = $form['#search_page'];
$redirect = $search_page['search_path'];
// Also encode slashes so we don't get akward situations when obtaining the
// search key. We can't use drupal_encode_path because for "aestetic" reasons
// they don't encode slashes...
$redirect_value = rawurlencode($form_state['values']['keys']);
if (strlen($form_state['values']['keys'])) {
$redirect .= '/' . $redirect_value;
}
$get = array();
if (isset($form_state['values']['get'])) {
$get = json_decode($form_state['values']['get'], TRUE);
}
if (!empty($form_state['values']['retain-filters'])) {
// Add our saved values
$get['retain-filters'] = '1';
}
else {
// Remove all filters
if (!empty($search_page['settings']['apachesolr_search_allow_user_input'])) {
unset($get['fq']);
}
if (module_exists('facetapi')) {
unset($get['f']);
}
}
if(!empty($form_state['values']['resource_type'])){
$get= $form_state['values']['resource_type'];
$get['search_content_type'] = 'search_content_type';
}
// var_dump($get);exit;
// Add the query values into the redirect.
$form_state['redirect'] = array($redirect, array('query' => $get));
}
2)对搜索进行处理 ,见下面红色注释部分以下的代码,是对 上面 的表单的内容类型的部分进行过滤
function custom_one_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
// Force an AND keyword search.
// Use 1 to force OR (the default), 100% to force AND
$mmvalue = $query->getParam('mm');
if(isset($mmvalue)) {
$query->replaceParam('mm', '100%');
}
else {
$query->addParam('mm', '100%');
}
$content_types = array_keys(node_type_get_types());//得到所有内容类型
foreach($content_types as $key=>$value){
if(!user_access('view any '.$value.' content')){ //没有访问此内容类型的权限,就把它从搜索中移除
$query->addFilter('bundle', $value, TRUE);
}
}
//依据内容类型来过滤
if(!empty($_GET['search_content_type'])){
$search_all_cont_typs = apachesolr_get_index_bundles('solr', 'node');
//$search_all_cont_typs
// 0 (String, 7 characters ) article
// 1 (String, 15 characters ) wp_blog_clone_1
// 2 (String, 15 characters ) wp_blog_clone_2
$search_want_cont_typs = array();
foreach($search_all_cont_typs as $key=>$search_all_cont_typ){
if(!empty($_GET[$search_all_cont_typ])){
$search_want_cont_typs [] = $search_all_cont_typ;
}
}
$exclude_types = array_diff($search_all_cont_typs,$search_want_cont_typs);
foreach($exclude_types as $key=>$exclude_type){
$query->addFilter('bundle', $exclude_type, TRUE);
}
//下面代码其实是有作用的
//下是几行是排除某一个 id ,的搜索
// $nid = 11022;
// $exclude = TRUE;
// $filter = apachesolr_site_hash() . '/node/' . $nid;
// $query->addFilter('id', $filter, $exclude);
//下面代码其实是有作用的
//下面是 可以实现 某一个 分类 taxonomy term 的搜索
// $query->addFilter('tid', 16);
}
}