Imagine for a moment that you are an administrator of a Drupal-based website. You have a page (or more) that you don’t want visible in your search results, but you don’t want to have gaps where those pages are missing either. You don’t necessarily want to implement authentication to block access to the pages, you just want to let folks know that there were one or more search results that aren’t necessarily public. Well then, I’ve got a tip for you.
One way to solve the problem might be to return a message saying something like “This search result item is private”. This way, the page count will still work, and people can know that there are actually results that they thought might be there, though they can’t see them at the moment.
To accomplish this, we will add in an ability to catch/intercept the Drupal pages (or “nodes”), so that a courtesy message can be displayed instead of the actual link and summary for the node.
By the way, all the examples below are based on a Drupal site running in cPanel, with the Search module version 7.18 (if that matters). And, in my method, you need to know the “node” ID for the items you wish to block. Just lettin’ ya know.
First, we have to do an “override” of the default Drupal behavior. In order to do this, we must put a template file in place, and then customize it. So we’ll copy the
search-result.tpl.php template file from default location into your theme templates folder, like this:
cp ~/public_html/modules/search/search-result.tpl.php .
In this fictitious example, you want to stop 3 nodes/pages/items from displaying; nodes 100, 101, and 102. Remember that while these might be pages with semi-private search result listings, if folks have the direct URL, they can still get to these pages. How does this really work? Well, you can cut to the chase and just compare this code with what’s in the default
search-result.tpl.php template file; but I’ll explain a bit below the code:
?> <li class="<?php print $classes; ?>"<?php print $attributes; ?>> <?php print render($title_prefix); ?> <h3 class="title"<?php print $title_attributes; ?>> <?php if ($result["node"]->nid != 100 && $result["node"]->nid != 101 && $result["node"]->nid != 102) : ?> <a href="<?php print $url; ?>"><?php print $title; ?></a> <?php else: ?> <?php print "This returned search result is a private item"; ?></a> <?php endif; ?> </h3> <?php print render($title_suffix); ?> <div class="search-snippet-info"> <?php if ($result["node"]->nid != 100 && $result["node"]->nid != 101 && $result["node"]->nid != 102) : ?> <?php if ($snippet): ?> <p class="search-snippet"<?php print $content_attributes; ?>><?php print $snippet; ?></p> <?php endif; ?> <?php if ($info): ?> <p class="search-info"><?php print $info; ?></p> <?php endif; ?> <?php endif; ?> </div> </li>
So basically, you are just inserting a couple PHP “if” blocks. The first block is to catch any listed item titles that match the nodes, and to display only the courtesy message and not a URL; the second block is to catch the summaries and not display them at all.
Now that you’ve moved the file into plave and have edited it, you must clear the site cache. Go to the Performance menu item, and click the Clear All Caches button under Clear Cache. If you don’t do this, you probably won’t see the change actuated…
And that’s it! Enjoy. I hope it helps…