Maintaining Lightbox2 Templating

Lightbox2 is a very popular and powerful Drupal module. One of the helpful features is that you can render a Drupal node in a lightbox rather easily in either PHP or HTML. View detailed explanation. The issue is that when you click a link within that lightboxed content, you will lose the lightbox2 templating (e.g., if your lightbox template hides sidebars, they’ll only be hidden when the node loads. If you click a link within the lightbox, the new node will load and sidebars will appear). Read on for a quick fix.

Linking to a lightboxed node

Linking to a lightboxed node using PHP and Drupal’s l() function.

Linking to a lightboxed node using HTML

Explanation and Problem

That works really well to open up the node in a lightbox. What happens though, when you click a link in that node? Well, you stay within the lightbox as expected, but you lose the Lightbox templating. I suppose I jumped over that part. When content is rendered in a lightbox you have new template suggestions that you can use. On my Drupal 7 site I copied page.tpl.php to a file called page–node–lightbox2.tpl.php, which I cleaned up by removing most page elements except for the actual page content (I didn’t want the menus, footer, sidebars, etc. appearing when a node is viewed in a lightbox). So, back on track… the initial node gets loaded using that new template file, but when you click a link in that lightboxed node, it opens with the normal page.tpl.php. The following probably isn’t the cleanest solution, but it does make sure the link gets loaded with the lightbox template.


This method uses a GET parameter, which isn’t the best thing. Unfortunately it means if a user knew the trick, they could show ANY page in lightbox form by appending ?lboxcustom=1 to any URL. If I come up with a fix for this I’ll edit this page and remove this paragraph. (I’m sure there’s a way).

Also, this method adds the GET param to EVERY link, regardless of whether or not it’s a link within the Drupal site. I’d be easy enough to target only internal links, but I don’t need that functionality here. Sorry 😉

Leave a Reply

Your email address will not be published.