Drupal Search Nodes and Comments

Build a forum search page that searches both nodes and comments with no extra modules or coding.

Whilst building a new Drupal site, I ran into a problem. 

This site needed a forum, so I installed the advanced forum module and proceeded to build out and style a forum. Like you do. Then, I got to looking at the search page. The included forum search view handily allowed you to filter by sub-forum, which was nice. But it made no distinction between different responses in a thread.

Say you're looking for the word 'nibknack'. Say there's a thread on your forum called 'funny words' and nibknack is mentioned in the 842nd of over 1000 replies. The forum search tells you that your search term appears in the funny words thread, and points you unhelpfully to the beginning of the thread. The reason for this is that generally speaking Drupal associates comments with nodes. In a forum context, the original post in a topic is a 'node' and every reply is a comment. 

Drupal views is a powerful tool, but I ran across one limitation almost immediately. Views are about content (nodes), or users, or comments - but not about more than one. I spent a couple hours looking through Drupal forums in vain for a simple way to search nodes and comments in with one form, with little success. Then I got one hint - if you have more than one exposed form on a page, populating one populates all of them.

So, here's how to create a search page that looks like one form, and that searches both comments and nodes. This example is aimed at Drupal forums, but could be used many ways.

Step One: Forum Search Page

Create a basic page with no content, give it a URL alias something like forum/search.

Step Two: Comment Search View Block

Create a new view, call it forum comment search. Select show "comments" and check the create a block checkbox. Call it Comment Search. Click Continue and edit.

Add your fields. I added comment title, author, post date and comment. (Comment is the body of the comment.) I set the comment body field to 'exclude from display' so as not to clutter up my search results - but that's optional.

Add your filters. Along with the comment: approved I added a content type = forum topic to limit it to just forum posts. Then I added a taxonomy filter to allow users to search by forum, since there are several sub-forums on the site. Pick "Has taxonomy terms (with depth) and select your forum taxonomy. I set the depth to 1 and checked the "expose to visitors" check box. Finally, add a Global: Combine fields filter. Set this to exposed, change the label to Search or something more appropriate, set the operator to 'Contains all words' (or whatever works for you) and select the title, author and comment fields for filtering.

Make sure Use AJAX (in the advanced section) is set to yes. I also usually set the exposed form settings to include reset button and autosubmit, but that's a personal preference.

Save that and move on.

Step Three: Node Search View Page and Block

Create a new view, call it forum node search. Select show "content" of type "Forum topic" and check both the page and block checkboxes. Put the URL for the forum search page you created in step one into the path field for the view page. Select unformatted list and fields for the Display Format options. Click continue and edit.

On the page display, add fields just like for the comment view. However, you'll have to add the author relationship to get the author name. I added Content: Title,(author) User: Name (by ), Content: Post date (Created), and Content: Body (Node Body). Like with the comments view, I set the body field to excluded from display.

Add filters. Same as before - content: type =  forum topic, content: has taxonomy, and global: combine fields filter. These will automatically be added to both displays. Make sure that the taxonomy and combine field filters are set to exposed.

Since you've already clicked on advanced to add the author relationship, move over and click on the "Exposed form in block" and click it. Set that to yes, and then set Use AJAX to yes as well.

Check the settings on the block display, including the Use AJAX, and then click save.

Step Four: Set it all in place

The views you created themselves created three blocks. A comment search block, a node search block, and an exposed form block. Go to admin/structure/block and configure your blocks. The three blocks all need to be set to appear on the page you created in step one. Put the exposed form block on top, and then the node and comment search blocks below. I put them in two regions, but whatever works for your theme. Set the titles to <none> and have them only appear on /forum/search.

Now, if you go to /forum/search, you'll see an exposed form with a search text field and a drop down for all your forums. Then, you'll see the node seach block (with its exposed filters) and then the comment search block with its exposed filters. If you type anything into the top filter, you'll see it appear in the others, and then each block will serve up its search results.

Just use some css to hide the exposed filters on the second and third blocks, and now you have one page that searches nodes and comments, with no extra modules or coding. The only thing left is to tidy up the results, rename or remove labels, and so on.

Posted by Buckethead Buckethead on   |   § 0

[ You're too late, comments are closed ]