How To: Add focused Drupal search to your site
When it comes to search, Drupal seems to do OK by itself. However, there are a number of supporting modules which will make your Drupal's default search even better. These include Porter-Stemmer (english only), Search 404, Search by Page, Similar By Terms and many others.
If you're seekign to help an advanced user out, then modules like Search config can help with that. But what about the user who won't dare go into the hidden area of 'Advanced Search'? This is where the power is - right?
It sure is. This is where you tell Drupal what content types and categories you want to limit the search to. This is where a user, simply looking for a job on your site, which lists information about jobs, news, blogs and other items, can focus their results.
So, why don't you stop expecting the user to figure this out, and just make it happen for them! That's what this video is all about. Using the default Drupal search box and forcing it to focus on specific content types or categories. You control what Drupal searches for and you control where it shows up!
Search Related Modules
- Porter-Stemmer [issues] - [usage]
- Search 404 [issues] - [usage]
- Search by Page [issues] - [usage]
- Similar By Terms [issues] - [usage]
- Search config [issues] - [usage]
- Faceted Search [issues] - [usage]
The Apache Solr project is what is used on Drupal.org. You know, where you get the cool faceted results from a search request.
Of course, if you haven't heard about it already, the Acquia Search service make it quite easy to take advantage of the Apache Solr coolness.
Here's the code.
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">// All of the following code goes within template.php<br><br>/*<br> * Using one of the preprocess hooks in template.php, you create a<br> * variable which calls drupal_get_form on the function which creates<br> * the search form<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB"><span class="caps">THEME</span></span><span style="color: #007700">-</span><span style="color: #0000BB"><span class="caps">NAME</span></span><span style="color: #007700">-</span><span style="color: #0000BB">HERE_preprocess_node</span><span style="color: #007700">(&</span><span style="color: #0000BB">$vars</span><span style="color: #007700">, </span><span style="color: #0000BB">$hook</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$vars</span><span style="color: #007700">[</span><span style="color: #DD0000">'focused_search'</span><span style="color: #007700">] = </span><span style="color: #0000BB">drupal_get_form</span><span style="color: #007700">(</span><span style="color: #DD0000">'<span class="caps">THEME</span>-<span class="caps">NAME</span>-HERE_focused_search_form'</span><span style="color: #007700">);<br>}<br><br></span><span style="color: #FF8000">/*<br> * This function will create a search form which adds a validation<br> * handler which you use to append any custom search criteria<br> * Note to self: Context module could be used here?<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB"><span class="caps">THEME</span></span><span style="color: #007700">-</span><span style="color: #0000BB"><span class="caps">NAME</span></span><span style="color: #007700">-</span><span style="color: #0000BB">HERE_focused_search_form</span><span style="color: #007700">(&</span><span style="color: #0000BB">$form_state</span><span style="color: #007700">, </span><span style="color: #0000BB">$keys </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Render the form to search a focused content type<br> </span><span style="color: #007700">if( </span><span style="color: #0000BB">module_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'search'</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$form </span><span style="color: #007700">= </span><span style="color: #0000BB">search_form</span><span style="color: #007700">(</span><span style="color: #0000BB">$form_state</span><span style="color: #007700">, </span><span style="color: #DD0000">'/'</span><span style="color: #007700">. </span><span style="color: #0000BB">drupal_get_path_alias</span><span style="color: #007700">(</span><span style="color: #0000BB">$_GET</span><span style="color: #007700">[</span><span style="color: #DD0000">'q'</span><span style="color: #007700">])); </span><span style="color: #FF8000">// Use existing search form code<br> </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'#validate'</span><span style="color: #007700">] = array(</span><span style="color: #DD0000">'search_form_validate'</span><span style="color: #007700">, </span><span style="color: #DD0000">'<span class="caps">THEME</span>-<span class="caps">NAME</span>-HERE_focused_search_validate'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'#submit'</span><span style="color: #007700">] = array(</span><span style="color: #DD0000">'search_form_submit'</span><span style="color: #007700">);<br> return </span><span style="color: #0000BB">$form</span><span style="color: #007700">;<br> }<br>}<br><br></span><span style="color: #FF8000">/*<br> * The validation handler added within the search form is where you make<br> * any adjustments to the search handled by Drupal. You can use any<br> * critera you wish, including the path or other variables available<br> * within Drupal.<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB"><span class="caps">THEME</span></span><span style="color: #007700">-</span><span style="color: #0000BB"><span class="caps">NAME</span></span><span style="color: #007700">-</span><span style="color: #0000BB">HERE_focused_search_validate</span><span style="color: #007700">(</span><span style="color: #0000BB">$form</span><span style="color: #007700">, &</span><span style="color: #0000BB">$form_state</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Using an array to determine which content type to search based on path from search form<br> </span><span style="color: #0000BB">$content_type </span><span style="color: #007700">= </span><span style="color: #0000BB">array_search</span><span style="color: #007700">(</span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'#action'</span><span style="color: #007700">], array(<br> </span><span style="color: #DD0000">'vendor_item' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'/vendors'</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'job_item' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'/jobs/sites'</span><span style="color: #007700">,<br> )<br> );<br> </span><span style="color: #0000BB">$content_type </span><span style="color: #007700">= empty(</span><span style="color: #0000BB">$content_type</span><span style="color: #007700">) ? </span><span style="color: #DD0000">'' </span><span style="color: #007700">: </span><span style="color: #0000BB">$content_type</span><span style="color: #007700">;<br> </span><span style="color: #FF8000">// Initialize using any existing basic search keywords.<br> // taken from node.module node_search_validate() function<br> </span><span style="color: #0000BB">$keys </span><span style="color: #007700">= </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">[</span><span style="color: #DD0000">'values'</span><span style="color: #007700">][</span><span style="color: #DD0000">'processed_keys'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$keys </span><span style="color: #007700">= </span><span style="color: #0000BB">search_query_insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$keys</span><span style="color: #007700">, </span><span style="color: #DD0000">'type'</span><span style="color: #007700">, </span><span style="color: #0000BB">$content_type</span><span style="color: #007700">); </span><span style="color: #FF8000">// additional seach criteria added here<br> </span><span style="color: #0000BB">form_set_value</span><span style="color: #007700">(</span><span style="color: #0000BB">$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'basic'</span><span style="color: #007700">][</span><span style="color: #DD0000">'inline'</span><span style="color: #007700">][</span><span style="color: #DD0000">'processed_keys'</span><span style="color: #007700">], </span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">$keys</span><span style="color: #007700">), </span><span style="color: #0000BB">$form_state</span><span style="color: #007700">);<br>}<br></span><span style="color: #0000BB">?></span></span>
Related videos: