开发者

Hide Drupal nodes from search

I made a private section on a drupal site by writing a module that checks the RERQUEST_URI for the section as well as user role. The issue I am running into now is how to prevent those nodes/views from appearing in the search.

The content types used in the private section are used in other places in the site.

What's the best way to g开发者_StackOverflowet Druapl search to ignore the content/not index/not display it in search results?


There is a wonderful article that explains just this on the lullabot site.

It's worth reading the comments to the post too, because people there suggested alternate ways of doing that, also by mean of contrib modules (rather than implementing some hooks in your own code). Code for D6 is in the comment as well.

HTH!


The lullabot article is a bit outdated and contains many blunt approaches. It also contains the answer in the comments - the Search Restrict module which works for DP6 and allows fine-grained and role-based control. Everything else either prevents content from being indexed, which may not be desirable if there are different access levels to content, or affects all search queries equally, which will also not work if there are different access levels.


If the content types used within the Private section are also used elsewhere how are you hoping to filter them out of the search results (note that I've not looked at the lullabot article by mac yet).

Basically, if you look at the details of two nodes, one private and one public, what differentiates them?

Note: I'm assuming that you want the nodes to appear to users with access to the Private area but not to 'anonymous' users.


For Drupal 7. You can hide the node from search results by using custom field. In my case, I have created a custom field in the name of Archive to the desired content type and with the help of that custom field you can write the my_module_query_alter functionality.

Code

function my_module_query_alter(QueryAlterableInterface $query) {
 $is_search = $is_node_search = FALSE;
 $node_alias = FALSE;
 foreach ( $query->getTables() as $table ) { 
   if ( $table['table'] == 'search_index' || $table['table'] == 'tracker_user') {
     $is_search = TRUE;
   }
   if ( $table['table'] == 'node' || $table['table'] == 'tracker_user') {
     $node_alias = $table['alias'];
     $is_node_search = TRUE;
   }
 }

 if ( $is_search && $is_node_search ) {
   $nids = [];
   // Run entity field query to get nodes that are 'suppressed from public'.
   $efq = new EntityFieldQuery();
   $efq->entityCondition('entity_type', 'node')
     ->fieldCondition('field_archive', 'value', 1, '=');
   $result = $efq->execute();
   if ( isset($result['node']) ) {
     $nids = array_keys($result['node']);
   }
   if ( count($nids) > 0 ) {
     $query->condition(sprintf('%s.nid', $node_alias), $nids, 'NOT IN');
   }
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜