Drupal core is missing a helper function to initialize the pager system for theme('pager'). Currently in D7 you must run an SQL query through the execute() method of class PagerDefault. Inside that method, it mucks with various GET params and global variables. We should make that part of the code separate from the class so that it can be re-used by other implementations of SQL or non-SQL based paging through results.

Comments

pwolanin’s picture

Status: Active » Needs review
StatusFileSize
new2.58 KB

motivation is this issue for apachesolr in D6: #667110: Replace bogus use of pager_query()

Should be a simple cut-n-paste patch moving code within pager.inc and wrapping some of it in a public API function. Let's see if this is working in all tests.

catch’s picture

Patch looks good, let's see what the test bot says.

Status: Needs review » Needs work

The last submitted patch, , failed testing.

pwolanin’s picture

Status: Needs work » Needs review
StatusFileSize
new3.01 KB

oops - forgot to move the global variabl declarations.

Status: Needs review » Needs work

The last submitted patch, , failed testing.

Status: Needs work » Needs review

Re-test of from comment #2405080 was requested by @user.

pwolanin’s picture

odd - those fails relate to filefield and not anything in the pager. Probably false.

Status: Needs review » Needs work

The last submitted patch, , failed testing.

pwolanin’s picture

Status: Needs work » Needs review

I don't see this failure locally - wth?

Re-test of from comment #2405080 was requested by @user.

agentrickard’s picture

I think the patch is ok; testbot was glitchy. I'm seeing fails locally, but I don't think they are related to this patch.

moshe weitzman’s picture

Hmmm. Perhaps rename to pager_default_initialize since we deliberately left the door open in D7 to alternate pager classes.

pwolanin’s picture

StatusFileSize
new3.02 KB

renamed function

agentrickard’s picture

Status: Needs review » Reviewed & tested by the community

Patch applies and works. Might be nice to add some docblock comments about how to add a custom pager.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Yes, let's get some how-to PHPDoc in here before this is committed.

Also, is there any particular reason we don't just make this part of the constructor of PagerDefault? Then PagerSuperAwesome could just do a similar thing in its constructor.

cburschka’s picture

A few minor quibbles about comments, and one about code flow:

+++ includes/pager.inc
@@ -171,6 +158,39 @@ class PagerDefault extends SelectQueryExtender {
+ *  The total number of items found.

"found" is kind of jarring here, since we're talking about items in general, not search results.

+++ includes/pager.inc
@@ -171,6 +158,39 @@ class PagerDefault extends SelectQueryExtender {
+ *  The number of items you will display per page.

Who is "you"? The code, the server or the developer? ;)

+++ includes/pager.inc
@@ -171,6 +158,39 @@ class PagerDefault extends SelectQueryExtender {
+  $page = isset($_GET['page']) ? $_GET['page'] : '';
+
+  // Convert comma-separated $page to an array, used by other functions.
+  $pager_page_array = explode(',', $page);

It's no real performance overhead, but do you really need to re-populate the global variable on every call? Just do it once or something...

This review is powered by Dreditor.

pwolanin’s picture

@webchick

we have to execute a query:

    $total_items = $this->getCountQuery()->execute()->fetchField();

so I don't think this is appropriate for the constructor.

cburschka’s picture

Well, PagerDefault is a class because it extends PDO. If we conclude that all pagers should be classes, then what would "PagerWithoutDatabase" extend? It can't exactly extend the SelectQueryExtender like PagerDefault does. (Nor can we make both extend a common Pager class, since that would involve PagerDefault extending two classes.)

In fact, the "PagerWithoutDatabase" pager would be completely unrelated to the database part, so I'm not sure why it should be a class.

pwolanin’s picture

Right, that's why I rolled this patch - for pagers that are totally unrelated ot the database.

I think what webchick meant (???) was an alternate DB pager class should inherit as much as possible. But I think this patch helps that too, since it reduces the # of LOC that might have to be reproduced in a variant implementation.

pwolanin’s picture

Status: Needs work » Needs review
StatusFileSize
new4.81 KB

I've broken this out into a second helper function (see the code examples) and added 2x code examples in the doxygen. I'm not sure what format we are using in D7 for these - there is no real use case in core, so the code is hypothetical.

Status: Needs review » Needs work

The last submitted patch, pager-init-667112-20.patch, failed testing.

pwolanin’s picture

Status: Needs work » Needs review
StatusFileSize
new4.8 KB

oops rolled patch with the wrong options

MichaelCole’s picture

#22: pager-init-667112-22.patch queued for re-testing.

pwolanin’s picture

This patch now has the doxygen that was missing at #15 - back to rtbc?

HedgeMage’s picture

Status: Needs review » Reviewed & tested by the community

This looks RTBC to me...thanks to webchick for prodding on the documentation, it was very much needed. :)

chx’s picture

Oh my god what a useful patch, half of the very simplistic Views backends I wrote are repeatitions of this patch, yes, yes please do!

klausi’s picture

Status: Reviewed & tested by the community » Needs work
+++ includes/pager.inc
@@ -73,22 +72,10 @@ class PagerDefault extends SelectQueryExtender {
+    $this->range($current_page * $this->limit, $this->limit);
+    ¶
     // Now that we've added our pager-based range instructions, run the query normally.

trailing whitespace

Powered by Dreditor.

pwolanin’s picture

seems also patch doesn't apply cleanly. "Hunk #2 FAILED at 72."

pwolanin’s picture

Status: Needs work » Needs review
StatusFileSize
new4.79 KB

Looks like just a whitespace change kept the patch from applying.

David_Rothstein’s picture

StatusFileSize
new7.08 KB

In addition to the whitespace issues, the attached version:

  • Adds even more exciting PHPDoc comments (because pagers are confusing and need all the docs they can get), and updates the example code a bit for D7 changes that occurred in the interim.
  • Clarifies the documentation for the return value of pager_find_page().
  • No longer uses a global variable inside pager_find_page() - turns out we didn't need it there, but only in the other function, and the fewer global variables the better.

Other than that, I've reviewed and tested this patch pretty carefully, and it's great. The actual code changes are pretty simple - mostly just moving existing code out into reusable functions - but the benefits are huge. In addition to Solr, the field API is another application in D7 where this is useful. Example: We are trying to build a D7 media gallery and want to page through the results of a multivalued media field on a node. We can't use the PagerDefault extender for that since field API queries are not tagged (and even if they were tagged, it wouldn't be a good idea, since the field API is supposed to be storage-agnostic).

This patch makes things work perfectly for that purpose, so hopefully we can get it in.

pwolanin’s picture

Status: Needs review » Reviewed & tested by the community

Given that this has extra code comments and the only code changes from before are white space and removing the unneeded global, and all tests pass, I think this is back to RTBC.

chx’s picture

This patch can't be committed. Really. What would happen to our cushy Drupal consultants jobs if we would make pager easy to use? Come on. First you remove the seven arguments of l() now it's pager globals? At this pace it won't take another 4-6 releases before any Dick or Harry can write Drupal code. We can't have that.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.