It is not possible to display multiple pagers on the same page as the pager element value is protected and can not passed to the theme_pager function (it will evaluate all pagers as element 0)
the only workaround is to use hard coded pager elements like
$query = db_select(..)->extend('PagerDefault')->element(42);
$build['mypager'] = array('#theme' => 'pager', '#element' => 42);
but the real solution would be to use the element from the query as it is already unique
$query = db_select(..)->extend('PagerDefault');
$build['mypager'] = array('#theme' => 'pager', '#element' => $query->element);
it is even in the documentation:
* After running this query, access $this->element to get the element for this
* query.
*/
protected function ensureElement() {
Comments
Comment #1
pasqualleComment #2
ff1 commentedSo... what does this patch actually do?
Comment #3
mattyoung commented.
Comment #4
pasqualle@ff1: you can access and use $query->element. Display the pager associated to given $query..
Comment #5
ff1 commentedAh... Simple! Thanks Pasqualle.
Seems like a no-brainer, especially as its already documented. My only problem is I don't really know how to test this. What's a simple way to introduce multiple pagers on the same page?
Comment #6
pasquallesimple example:
Comment #7
sunMakes sense.
I guess there should be some API documentation somewhere in code that needs to be updated?
Comment #8
bojanz commentedYes, definitely!
While we're at it, could we unprotect $maxElement too?
I did it in #885014: Add pager and tablesort to EntityFieldQuery since an alternative pager implementation needs to modify it in order not to conflict with the existing pager implementation on the same pager, however I'm not sure that #885014: Add pager and tablesort to EntityFieldQuery will land in D7...
That all together gives us much more flexibility in working with pagers...
Comment #9
crifi commentedBump it to the highest version. This needs work because of new dev version and git.
Comment #10
crifi commentedIs this already fixed? See #903110: Multiple pager support is partially broken.
Comment #11
cweagansFixing tags per http://drupal.org/node/1517250
Comment #12
pasqualleThe element is still protected in D7. Not sure where it is in D8..
Comment #13
dawehner.
Comment #16
daffie commentedWe have closed #2572533: Add tests for multiple pagers on a given page. So for D8 this is fixed. Moving this to D7.
Comment #18
pasqualleComment #19
pasqualleComment #20
poker10 commentedIn D9 a new method was added to access the
$elementproperty, see: #1202484: Improve usage of 'pager.manager' service in PagerSelectExtender, allow code to know the pager element ID used.Wouldn't it be a better approach than changing the property access modifier?
Comment #21
poker10 commentedSomething like this. But let @mcdruid decide what approach will be better for D7.
Comment #22
mcdruid commentedHmm this is a little tricky.
The code comment that mentions accessing the element is on a protected function
\PagerDefault::ensureElement():I think adding an accessor method per #20/21 is the "correct" way to do this (and would be a correct backport from D8/9), but the simple patch in #1 is more than 12 years old and any sites that have applied that patch would need to change their theme code to use a new accessor instead.
Is there any reason we couldn't do both; the correct and the pragmatic change combined?
I'll ask for Fabianx's opinion as Framework Manager, but I'd be happy to commit both patches (#1 and #21).
Comment #23
mcdruid commentedFabianx's framework manager review:
I agree with that, so #21's RTBM.
Comment #25
mcdruid commentedAdding credit for the review.
Comment #26
poker10 commentedDrafted a change record here: https://www.drupal.org/node/3307777 (some parts taken from the D8 CR).
I was thinking that it would be good to also add tests from D8, so we can actually verify that the new function is working and returning correct ID. See D8 commit: https://git.drupalcode.org/project/drupal/-/commit/96dc01cf37f3a4570540bbd6ec6eff09c47d29a9
I have updated the patch - only checks with
getElement()function are added. Reuploading the patch to verify. Please re-review, so we can commit this.Comment #27
mcdruid commented#3307795: Drupal 7.x core tests dissappeared for the current D7 drupalci testbot problem.
In the meantime, I ran D7 tests for 702778-26.patch on a local drupalci with the default PHP 7.4 and MySQL 5.7 and all tests passed.
I'll review the patch manually shortly and put it back to RTBC unless I spot any problems.
Comment #28
mcdruid commentedThis looks good, and is low risk as it's just adding the accessor (plus tests, which is great)!
Comment #30
mcdruid commentedThanks everybody; great to close an issue with a 6-digit nid as Fixed!