Problem/Motivation
All pagers get a unique element ID by default (in execute()), but this ID is never revealed to the user, so it cannot be used when displaying the pager with
$page[] = array(
'#theme' => 'pager',
'#element' => $pagerid, // <= unknown!
);
However it is nice to allow users to set the element ID with the element($id) function, but as they might never know which tables and pagers are displayed at once, it is not very safe to use numbers 1, 2, 3, etc. (as it is not possible to use string names as ID).
Proposed resolution
I suggest to enrich the PagerDefault class with this public function:
/**
* Return element ID for this pager query. If not set yet, it sets it.
*/
public function getElement() {
$this->ensureElement();
return $this->element;
}
This will make it possible to have automatically "named" pagers and display them correctly. And I don't think the element($id) function will be used anymore.
In case the expected pager usage (mainly displaying the right pager) is different as I think and this function is not needed, please explain, how to display two pagers without setting arbitrary element id number by element($id) function. (Thanks)
Remaining tasks
Commit
User interface changes
None
API changes
- The new method Drupal\Core\Database\Query\PagerSelectExtender::getElement() which returns the element ID for the pager query.
- The new method Drupal\Core\Database\Connection::getPagerManager() which returns the pager manager service.
- The method Drupal\Core\Pager\PagerManager::getMaxPagerElementId() has been made public (was protected).
- The new method Drupal\Core\Pager\PagerManager::reservePagerElementId() which reserves a pager element ID.
Data model changes
None
Release notes snippet
TBD
Comment | File | Size | Author |
---|
Issue fork drupal-1202484
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 1202484-code-can-not changes, plain diff MR !103
Comments
Comment #1
marcingy CreditAttribution: marcingy commentedFeatures are commited in latest version first.
Comment #2
c4rl CreditAttribution: c4rl commentedSubscribe. We should really use string names. Seems blatantly obvious.
Comment #3
kingandy CreditAttribution: kingandy commentedString names would make it lengthier to pass multiple pager states back and forth (at the moment it's a simple "page=0,0,1,0,3" in the query strings), but yes, much easier to keep track of as a developer.
Comment #4
Michael Zetterberg fd. Lopez CreditAttribution: Michael Zetterberg fd. Lopez commentedI like the getter approach better than just making the element property public like in #702778: Multiple pagers on the same page.
You will still need access to the pager instance to use this feature request, which you do not have sometimes for various reasons. What I do right now to solve my problem (even though I have access to the pager instance) is to read
PagerDefault::$maxElement
. The value returned from the method in this feature request is the same asPagerDefault::$maxElement - 1
right after invokingexecute()
on the pager.I have attached 2 patches, one for D7 and one for D8. Maybe that will help move this issue towards solved.
Comment #5
Michael Zetterberg fd. Lopez CreditAttribution: Michael Zetterberg fd. Lopez commentedForgot the status...
Comment #6
heddnThere's some whitespace issues.
In these lines.
Comment #7
marcingy CreditAttribution: marcingy commented4: d8-identify-pagers-1202484-0.patch queued for re-testing.
Comment #8
Rajendar Reddy CreditAttribution: Rajendar Reddy commentedUpdating patch with reroll. Please review.
Comment #9
dawehnerI guess we need some tests
Comment #10
Mile23Patch still applies.
Comment #11
Mile23I'd say
ensureElement()
should return the element, and thengetElement()
would just call it. Or really just change the name ofensureElement()
togetElement()
and make it public.Either way it's probably an API change, so it might not make it in during RC.
The patch still applies, but it still needs tests.
Comment #12
dawehnerWell, its an API addition, but yeah I don't see any reason why this could/should not be able to land in 8.0.x
Moving to the right component ...
Comment #13
mondrakeIt's a bit far off, but I ended up building this on top of #2044435: Convert pager.inc to a service, as I was trying to understand implications of converting the pager to a service to the PagerSelectExtender. As such this is for 8.1.x or later.
However I think the tests could be adjusted to work with the patch in #8 if anyone wants to give a try.
Do not test patch is on top of #2044435: Convert pager.inc to a service, full patch for the bot. No interdiff as it won't make so much sense.
Comment #14
mondrakeUpdated title.
Comment #17
daffie CreditAttribution: daffie commentedPostponed on #2044435: Convert pager.inc to a service.
Comment #18
daffie CreditAttribution: daffie commentedI think that the tests in SelectPagerDefaultTest with the extra tests being added is enough testing.
This issue relies on #2044435: Convert pager.inc to a service. And that issue is going to be fixed on Drupal 8.3.
Comment #19
mondrake@daffie I think we would need an intermediate step after #2044435: Convert pager.inc to a service before coming to this one: Refactor Database/Query/PagerSelectExtender to use the new service.
I haven't opened an issue for that yet, waiting for the milestone of #2044435: Convert pager.inc to a service before opening its follow-ups.
Comment #20
mondrakeOpened #2801285: [PP-1] Refactor PagerSelectExtender to use the pager.manager service.
Comment #27
mondrake#2044435: Convert pager.inc to a service is in! This can be unblocked now.
Comment #31
mondrakeMoved issue to Merge Request workflow, patch is practically a new one, reroll of the one in #13 wouldn't make sense.
Comment #32
mondrakeComment #33
daffie CreditAttribution: daffie commentedPatch looks good.
Added a couple of comments on the MR.
Comment #35
mondrakeAdded CR https://www.drupal.org/node/3194594
Comment #36
mondrakeComment #37
daffie CreditAttribution: daffie commentedAsking for your opinion @mondrake.
Comment #38
mondrakeCommented in MR
Comment #39
daffie CreditAttribution: daffie commentedReplied on the MR.
Comment #40
mondrakeLet's see what bot thinks.
Comment #41
daffie CreditAttribution: daffie commentedTestbot fails.
Comment #42
mondrakeComment #43
daffie CreditAttribution: daffie commentedComment #44
daffie CreditAttribution: daffie commentedUpdated the IS and the CR.
Comment #45
mondrakeComment in MR
Comment #46
daffie CreditAttribution: daffie commentedAll the code changes look good to me.
The IS and CR are in order.
All the new methods have testing.
For me it is RTBC.
Comment #47
catchComment #48
mondrakeComment #49
mondrakeComment #50
catchWe can probably deprecate without a trigger_error() in this case - we do that for global constants and similar too.
Comment #51
mondrakeAlright, but in this case we should have a BC shim to keep the property storing the real value, do we?
Comment #52
catchYes that's still needed.
Comment #53
mondrakeon this
Comment #54
mondrakeComment #55
mondrakeComment #56
daffie CreditAttribution: daffie commentedThe MR is failing the testbot.
Comment #57
mondrakeUm, latest changes show that the patch here is subtly flawed: $maxElement in HEAD now is increased on calling element(), while in the patch here the pager ID is only increased in execute() when the Pager object is created. This opens the door to bugs in case code opens multiple selects without executing them - they would all get the same pager id.
Can’t create a Pager object at the element() call, either, since we won’t have the needed args for that yet.
IMHO we need a kind of
reservePagerId
method on the PagerManager to take into account this, and probably manage the max pager Id state in a property there.Comment #58
mondrakeDone #57
Comment #59
daffie CreditAttribution: daffie commentedUpdated the IS and the CR with the new method Drupal\Core\Pager\PagerManager::reservePagerElementId().
Comment #60
daffie CreditAttribution: daffie commentedMoving it back to needs work for the nitpick.
Comment #61
mondrakeFixed the nit and optimized a bit
ensureElement
.Comment #62
daffie CreditAttribution: daffie commentedAll code changes look good to me.
All new methods have testing.
The public static variable is a good as it gets deprecated.
It is all documented in the IS and the CR.
For me it is RTBC.
Comment #64
catchThanks that looks better with the bc layer.
Committed 96dc01c and pushed to 9.2.x. Thanks!
Comment #66
mondrakeThis caused a small issue in contrib, which IMHO should be solved in a core test module: #3202014: pager_test_preprocess_pager() should return if pager element is not defined