Filtering Node Types In Drupal 6 Search

31st January 2011

A common practice when creating sites in Drupal is to create different node types for different purposes. Sometimes these node types can be functionality based rather than content based and are used for creating a rotating banner or something similar. A side effect of this is that you will then see these nodes appearing in search results, which can cause some confusing results to be displayed.

So how do you remove these nodes? Well with quite a simple little module you can intercept the search query and stop certain node types being searched for. Adding a couple of extra functions means that we can add form controls to the advanced search form and the search admin area so that nodes can be selected to be excluded from the search results.

First, create a folder in your sites/all/modules directory for the module (I called it searchfilter) along with an info file. We need to add a dependency to the search module so that this module doesn't cause any wierd side effects (errors really) when the search module it not present. Here is the searchfilter.info file.

  1. ; $Id$
  2.  
  3. name = Search filter
  4. description = "Filters out selected content types from search results"
  5. dependencies[] = search
  6. core = 6.x

Now create a file that will store the module code called searchfilter.module. This file will contain 4 functions, which I will now go over one by one. The first thing to do it set up a permission so that we can restrict or allow access to the node filter. This is a simple implementation of hook_perm().

  1. function searchfilter_perm() {
  2. return array('access searchfilter content');
  3. }

The next step is to add a call to HOOK_db_rewrite_sql() so that we can intercept the search query and add the needed where clause to prevent our selected nodes from appearing in the search results. This function makes sure that we are looking at the right query as well as providing a bypass for users who have the 'access searchfilter content' permission. This means that you can turn the module on and still allow some users to find anything they need to.

  1. function searchfilter_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  2. // Users with the correct permissions can search for all content on the site.
  3. if (!user_access('access searchfilter content')) {
  4. if ($query == '' && $primary_table == 'n' && $primary_field = 'nid' && empty($args)) {
  5. $excluded_types = variable_get('searchfilter_types', array());
  6. if (!empty($excluded_types)) {
  7. $where = " n.type NOT IN ('" . implode("','", $excluded_types) . "') ";
  8. return array('where' => $where);
  9. }
  10. }
  11. }
  12. }

In order to allow adminitrators to pre-select the node types to be excluded a hook is added to alter the search admin form. This adds a select box to the bottom of the form in the administration form that will just list out the node types.

  1. function searchfilter_search($op = 'search') {
  2. if ('admin' == $op) {
  3. $form = array();
  4. $form['searchfilter_types'] = array(
  5. '#type' => 'select',
  6. '#multiple' => TRUE,
  7. '#title' => t('Exclude Node Types'),
  8. '#default_value' => variable_get('searchfilter_types', array()),
  9. '#options' => node_get_types('names'),
  10. '#size' => 9,
  11. '#description' => t('Node types to exclude from search results.'),
  12. );
  13. return $form;
  14. }
  15. }

The final step is to add an option to the main search form (for users who have the permission) that will allow users to select from every node type available and not just those allowed.

  1. function searchfilter_form_alter(&$form, &$form_state, $form_id) {
  2. if ('search_form' == $form_id) {
  3. if (!user_access('access searchfilter content')) {
  4. $excluded_types = variable_get('searchfilter_types', array());
  5. $types = array_map('check_plain', node_get_types('names'));
  6. foreach ($excluded_types as $excluded_type) {
  7. unset($types[$excluded_type]);
  8. }
  9. $form['advanced']['type']['#options'] = $types;
  10. }
  11. }
  12. }

Once these two files are in place you will be able to activate the module and add a filter to your search results.

For convenience, here is the fully assembled code.

  1. <?php // $Id$
  2.  
  3. /**
  4.  * @file
  5.  */
  6.  
  7. /**
  8.  * Implementation of HOOK_perm().
  9.  *
  10.  * @return array An array of the permissions for this module.
  11.  */
  12. function searchfilter_perm() {
  13. return array('access searchfilter content');
  14. }
  15.  
  16. /**
  17.  * Implementation of HOOK_db_rewrite_sql. Rewrite the search database query and
  18.  * add in the node types that have been selected.
  19.  *
  20.  * @param string $query Query to be rewritten.
  21.  * @param string $primary_table Name or alias of the table which has the primary key field for this query.
  22.  * @param string $primary_field Name of the primary field.
  23.  * @param array $args Array of additional arguments.
  24.  *
  25.  * @return array The addition to the where statement to be used by the search
  26.  * query.
  27.  */
  28. function searchfilter_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  29. // Users with the correct permissions can search for all content on the site.
  30. if (!user_access('access searchfilter content')) {
  31. if ($query == '' && $primary_table == 'n' && $primary_field = 'nid' && empty($args)) {
  32. $excluded_types = variable_get('searchfilter_types', array());
  33. if (!empty($excluded_types)) {
  34. $where = " n.type NOT IN ('" . implode("','", $excluded_types) . "') ";
  35. return array('where' =?> $where);
  36. }
  37. }
  38. }
  39. }
  40.  
  41. /**
  42. * Implementation of HOOK_search, allows the addition of a form component to our
  43. * search admin interface.
  44. *
  45. * @return array The additional form component.
  46. */
  47. function searchfilter_search($op = 'search') {
  48. if ('admin' == $op) {
  49. $form = array();
  50. $form['searchfilter_types'] = array(
  51. '#type' => 'select',
  52. '#multiple' => TRUE,
  53. '#title' => t('Exclude Node Types'),
  54. '#default_value' => variable_get('searchfilter_types', array()),
  55. '#options' => node_get_types('names'),
  56. '#size' => 9,
  57. '#description' => t('Node types to exclude from search results.'),
  58. );
  59. return $form;
  60. }
  61. }
  62.  
  63. /**
  64. * Implementation of HOOK_form_alter. Alter the search form to restrict the
  65. * selectable node types to just the list that has been saved.
  66. *
  67. * @param array $form Nested array of form elements that comprise the form.
  68. * @param array $form_state A keyed array containing the current state of the form.
  69. * @param string $form_id String representing the name of the form itself.
  70. */
  71. function searchfilter_form_alter(&$form, &$form_state, $form_id) {
  72. if ('search_form' == $form_id) {
  73. if (!user_access('access searchfilter content')) {
  74. $excluded_types = variable_get('searchfilter_types', array());
  75. $types = array_map('check_plain', node_get_types('names'));
  76. foreach ($excluded_types as $excluded_type) {
  77. unset($types[$excluded_type]);
  78. }
  79. $form['advanced']['type']['#options'] = $types;
  80. }
  81. }
  82. }

Add new comment

The content of this field is kept private and will not be shown publicly.