Going back to paginated results page in Drupal with Views

Posted on January 12, 2011 at 4:45 pm

On a recent project we have a page (generated by Views) that has 3 panels with a listing, calendar and map. The map and calendar contains entries for all of the items on the list. However, the number items in the list requires pagination. On the detail page for each entry you can simply use the browser back button to return to the previous page, but you get a warning asking if you’re sure you want to resubmit the form, not ideal. I decided to implement a more elegant method of returning the previous page without having to resumbit the form.

Firstly, I needed to grab some information from the object returned by get_current_view(). I then added a dsm call to the the full PHP header so I could see what’s actually available. Here’s the code to put into the header of the view to get this information…
[php]
<?php
$view = views_get_current_view();
dsm($view);
?>[/php]

…and here’s a screenshot showing the code in place

Getting the current view information

I could see from the output that there were 2 elements I was interested in, $args=$view->args['0'] (which held the cached view id) and $page=$view->pager['current_page'] (which held the current page the user was on, starting at 0). so far so good.

Next, I also needed a way to concatenate several fields into a table cell (as that was how the output was needed). I drushed Views custom field so that I could rewrite the output of a field with PHP. Custom field is like Views global text on steriods, highly recommended.
After a little more jiggery pokery getiing the other items I needed, I ended up with the following code in my custom field:
[php]
<?php
$view = views_get_current_view();
$args=$view->args['0'];
$page=$view->pager['current_page'];
$title=$data->node_title;
$loc=$data->location_city;
$contact=$data->node_data_field_result_set_id_field_contact_value;
$rectype=$data->node_data_field_result_set_id_field_record_type_value;
$recid=$data->node_data_field_result_set_id_field_record_id_value;
echo "<h3><a href=’/record/$rectype/$recid/$args/$page’>$title</a></h3>";
echo "<h4>$loc</h4>";
echo "<p>".htmlspecialchars_decode($data->node_revisions_body)."</p>";
echo "<span class=’contact’>$contact</span>";
?>
[/php]

As you can see, this takes some of the CCK node values, the node title, node body and views arguments and wraps them up in a url like this:
[code]
/record/[rec_id]/[rec_type]/[view_cache_id]/[current_page]
[/code]

Finally, I needed to adjust my custom module to accept the 2 new parameters (view_cache_id and current_page). Having done that I just had to add a link in my template – I’d already set the vlause to the variables $ref and $page.
[php]
echo "<div class=’back-to-search’><< Go back to <a href=’/dir?$ref&page=$page’><strong>your search results</strong></a></p>";
[/php]

All done, and a much better user experience as well.

Tags: , ,

21 Responses to “Going back to paginated results page in Drupal with Views”

 
  1. afosaronaq says:

    Good.
    Thanks for your informative reply. I will read and study about the drug first….buy cheap online.
    Pa!..
    ____________________________
    generic pharmacy

  2. kesaroxev says:

    Good day!!!!!
    Hi Tinmuning, thank you for your useful answers and suggestions. I’ll better ask around in the forums..generic mail order!..
    Bye!..
    ____________________________
    order online

  3. bolervawos says:

    Greetings!!
    Thanks for your informative reply. I will read and study about the drug first.!!!!!best price generic.
    Bye!
    ____________________________
    get generic

  4. GERD says:

    Salutations…

    I thought sending this trackback great feature…

  5. rastityx says:

    Greetings…
    They do report feeling more tired in the beginning. Here are the side effects of this drug (remember these are possible side effects): indigestion, nausea, vomiting, diarrhea or constipation, dizziness, and can be toxic to the liver. The 3 things I think you need to know are: …online uk.
    Bye!..
    ____________________________
    uk generic

  6. Wordlistz2 says:

    Leeboxz2…

    Great blog post, saw on…

  7. orastisob says:

    Hi there!
    For males, at this age (17), it is still considered as your puberty stage where growth still continues up to age 23 or 24.!!!!buy on line generic…
    Bye!!!
    ____________________________
    buy cheap

  8. Snappointz2 says:

    Jabbertypez2…

    Wonderful blog post, saw on…

  9. Rhymiaz2 says:

    Leeboxz2…

    Wonderful blog post, saw on…

  10. Fine post…

    Thank you, I have recently been searching for information about this subject for a while and yours is the best I have found out so far. But, what in regards to the bottom line? Are you positive concerning the source?…

  11. Useful and precise…

    It is incredibly super hard to find real honest and quality informative and precise fresh info but today about noon I happily noted…

  12. Useful and precise…

    Its hard to find really informative and accurate information but here I noted…

  13. Just looking…

    obviously like your web site but you need to take a look at the spelling on quite a few of your posts. Many of them are rife with spelling issues and I to find it very troublesome to tell the reality however I will definitely come again again….

  14. Just Looking…

    When we were browsing very much last Monday we saw a very nice long article about…

  15. I ve enjoyed many of your posts, and I have yet to come across a series of sidebar web links as thorough or as extensive as yours.

  16. Bing results…

    While browsing Bing I found this page in the search results and I didn’t think it match…

  17. Owen Knower says:

    I simply want to mention I am just all new to weblog and certainly savored your blog site. Most likely I’m want to bookmark your website . You amazingly have terrific articles and reviews. Regards for sharing your website.

  18. I simply want to mention I am all new to blogging and certainly loved you’re page. More than likely I’m likely to bookmark your blog post . You actually have really good writings. Many thanks for revealing your website page.

  19. site says:

    Tumblr linked to this website

  20. fotiramyn says:

    Good afternoon!!!!!
    It should never be prescribed without an accurate medical assessment, and patients should always check with their physician or their pharmacist before taking any new medications, over the counter or herbal remedies included.!..what is generic!!!!!
    Bye!!!!
    ____________________________
    prices generic

  21. Great read…

    Thanks for the sensible critique. Me & my neighbor were just preparing to do some research on this. We got a grab a book from our area library but I think I learned more clear from this post. I’m very glad to see such great info being shared freely ou…

 

Leave a Reply