Pfew. I think the title says it all. This post is about a very specific thing. It’s actually not too complicated but it’s a useful thing to be able to do, this won’t be a long post.
Here’s what we’ll learn to do: we’ll learn to add a ‘back’ button to a views-based search you’ve created. The result of this will be that when your users search your site and a set of results are displayed…they will then be able to click a result and see the entry, with a back button. This back button, when clicked, will take the user back to the search results. Moreover the search results will be saved so that any keywords entered by the user and any filters selected will still be active and the results will still reflect those keywords and filters. This is very valuable for the user in many cases.
However, as a caveat: one thing I have noticed about this feature, that I have not yet figured out how to fix is that the keywords and filters selected stay set even AFTER you close and re-launch the browser a day or so later. So I’m trying to figure out how I can flush these caches for the user automatically once they leave the site. Just a session cookie would be fine. I’ve tried running cron and flushing the caches myself manually to see if that fixes the problem…it doesn’t. So I’m not sure if it’s stored in temporary table or what, but I’m still diagnosing this problem.
Ok, so onto the point of the post.
Adding a Back button to your Search Results
This is the quickest part. You simply need to add a button, with the following code, to your tpl file for your content type nodes. For me, for example, it was node–resource.tpl.php.
Here’s the code:
<?php
print ‘<div class=”go-back”><a href=”javascript: history.go(-1)”>
<< Go Back</a></div>’;
?>
That part’s easy enough. But I also didn’t want this button to show up every time a user went to a node. What if they went to a node directly, without using the search to get there? Then the back button would break for them and become a bad user experience. So I added this nugget:
if (strstr($_SERVER[‘HTTP_REFERER’], ‘yoursitename.com’) !== false) {
And the total code becomes:
<?php
// display GO BACK on profile but ONLY if user came from the site
directly
if (strstr($_SERVER[‘HTTP_REFERER’], ‘sitename.com’) !== false) {
// The site brought me to this page so display the ‘go back’ button.
print ‘<div class=”go-back”><a href=”javascript: history.go(-1)”>
<< Go Back</a></div>’; }
?>
Next, I had to determine how to preserve state for the search results. Currently if a user clicks the new back button you’ve added to the site the search results reset every time. Not ideal. So here’s how to preserve state:
Preserving State in View-Based Search Results
To fix your view go into your views administration area (admin > structure > views). Then ‘edit’ your search view. For the filters you want to preserve state for under ‘filter criteria’, click each one and select ‘remember the last selection’. Also choose which user roles this applies to here. Yes, it’s that easy.
Oh, and one other thing you need to do. Turn off Ajax if you have that on. Ajax makes the search a bit sexier but it will mess up your results with this option turned on. So make sure to turn off Ajax under ‘Advanced’. Set it to ‘no’.
Ok, that’s it. You’re now set to use your back button effectively. I’ll post a comment to this post when I figure out how to fix the state problem I addressed earlier.