Drupal Lightbox2 and Paging

So I had a strange problem with an ecommerce site Im developing. The site shows in the front page some products. The Owner told me to put a button with the magnifying glass over there, and when clicked, make it appear a modal box that shows more info of the product.

The Magnifying Glass in all it splendor

So hands at work, i use the lightbox2 and views module to make something like the owners command. The frontpage must show 8 products and if we have more, use a pager to navigate between products.

This is the pager, just in case youre asking what is a pager.

The problem? The view use paging so i show only the first eight products in the first page but if we go to the second page, problems occurs. The lightbox show all the modal boxes with the information of the first page. Oh god i smell some bug induced headache soon


The Solution
So after an entire day of debuging I found the problem: Lightbox2 is not taking in account the page you are when it retrieve the data. Basically lightbox2 use the load() function of jquery to get the WHOLE page it need to extract the info. The url of the page is inserted by lightbox2 usign the template in the file lightbox2_handler_field_lightbox2.inc

You have to change the line 124 like this:
Before:

// We don't actually use the link, but we need it there for lightbox to function.
if (empty($link)) {
// Get the path name.
$path = isset($_GET['q']) ? $_GET['q'] : '';
//Created By Carlos
}

After:

// We don't actually use the link, but we need it there for lightbox to function.
if (empty($link)) {
// Get the path name.
$path = isset($_GET['q']) ? $_GET['q'] : '';
//Created By Carlos
if(isset($_GET['page'])){
$path = $path .'?page='.$_GET['page'];
}
$link = url($path, array('absolute' => TRUE));
}


I just get the GET value of the current page and if it have a variable called page, append ?page= and the number of the page to the path that will be inserted in the lightbox trigger. Question anyone?

2 comments:

JayIsHappy said...

woa, Man, you saved my life \o/
thank you so much for this !

by the way, you have a little typo here :
if (emptyempty($link)) {

Jose Carlos Tamayo said...

No problem , glad to help, although I think the new version of lightbox as already fixed this. Anyway, enjoy the fix :D