- Issue created by @j. ayen green
- 🇳🇿New Zealand quietone
Does this apply to currently supported versions of Drupal? Drupal 9 is now EOL and is no longer receiving changes. Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.
This effect could be caused by, for example, unset cache metadata. For a bug report we would need to have a complete code example and an understanding of why automated tests of the pager in Drupal Core are invalid.
- 🇺🇸United States j. ayen green
I've recreated the issue in D10. The code (below) is quite simple. I can upload the view config, but it's simply a content view of articles set to display 10. I'm including a screen capture of xdebug.
<?php use Drupal\views\ViewExecutable; use Drupal\views\Plugin\views\query\QueryPluginBase; /** * Implements hook_views_query_alter(). */ function itemsperpage_views_pre_render(ViewExecutable $view) { if ($view->id() == 'itemsperpage') { $view->pager->setItemsPerPage('5'); $x = $view->pager->getItemsPerPage(); } }
I understand that the setter works.
setItemsPerPage
on the pager object does not set cache metadata. Did you try callingsetItemsPerPage
on the ViewExecutable object, which does?- 🇬🇧United Kingdom aaron.ferris
Im seeing this issue also,
$view->setItemsPerPage(5)
; has no effect in my hook_views_post_execute() implementation./** * Implements hook_views_post_execute(). */ function my_module_views_post_execute(ViewExecutable $view) { if ($view->id() == 'my_view') { $view->setItemsPerPage(5); } }
This is on a view of paged 50 items.
Surely the function works, because the UI can do it. As a non-Views expert all I can add is: Has anyone stepped-through in a debugger to figure out why the hook isn't effective?
- 🇮🇳India Akhil Babu Chengannur
This worked for me
$view->initPager(); $view->pager->setItemsPerPage(10);
- 🇮🇳India arunkumark Coimbatore
I can replicate the issue on Drupal Core 11.x views pager. Updating the issue reproduce steps.
Below is the observation from my side. The execution of the hooks and functions are as per below,
1. SQLBase.php -> updatePageInfo()
2. SQLBase.php -> createPager()
3. hook_views_post_execute()
4. hook_views_pre_render()
5. template_preprocess_pager()Am unable to alter the pagination item per page via the custom code.
- 🇮🇳India arunkumark Coimbatore
After spending some time found using
hook_views_query_alter()
./** * implements hook_views_query_alter(). */ function hook_views_query_alter(ViewExecutable $view, QueryPluginBase $query) { $view->setItemsPerPage(PAGE_PER_ITEM); }
- 🇮🇳India diwakar07
Able to reproduce the issue in Drupal 10.3.0.
I agree with #10.Hooks like
hook_views_post_execute()
orhook_views_pre_render()
tend to update the#items_per_page
key of the view but still renders the default pager value set in the view, rather than using the number set in custom code.
When usinghook_views_query_alter()
, the pager successfully gets updated.Thanks.
- Status changed to Closed: works as designed
5 months ago 3:06pm 7 August 2024 - 🇳🇱Netherlands Lendude Amsterdam
Yes, timing is everything. Updating the pager after the query has run, doesn't do anything useful. As shown in #10, changing this before the pager is used is the way to do this.
Could there be a way, based on object state, to throw an exception if methods like that are called uselessly?