SERVER-SIDE version of "remembering" user's last Drupal quicktab

Have Drupal remember a user's last QuickTab - on the SERVER side!
Have Drupal remember a user's last QuickTab - on the SERVER side!

In this blog post I gave an example of client-side script to remember the last Drupal QuickTab a user loaded so that when he came back to that page it was already selected again, instead of the default tab. It's a fine solution, but in some cases it's a drag to have that first tab grind the server when you are immediately going to flick over to a different tab anyway.

So here's a server-side version that stores the last tab in the $_SESSION variable - it will recall the last tab that *executed* (which is not necessarily the last on the user viewed) but in most cases that's perfectly acceptable. Put this in your Global PHP header in the view:

$_SESSION['last_tab_dashboard'] = 3;

...in this example, my view is on the 4th tab (tab number 3) of the quicktabs.

Then, on your page where you render your tabs (such as in a Panels page, for example) put the following:

$last_tab = 0;
if (isset($_SESSION['last_tab_dashboard'])) $last_tab = $_SESSION['last_tab_dashboard'];
$tabs = quicktabs_build_quicktabs('dashboard', array('default_tab' => $last_tab));
print drupal_render($tabs);

and *boom* your default tab is the "correct" one AS IT RENDERS and you don't even get the flickering of the first tab drawing first, etc. It's sublime!

Tags: