$view->pager->setItemsPerPage has no effect

Created on 22 July 2024, 3 months ago
Updated 7 August 2024, 2 months ago

Problem/Motivation

I have attempted to change the displayed items per page in a view (set in the UI to 10) by using
$view->pager->setItemsPerPage(5) and alternatively $view->pager->setItemsPerPage('5')
I have tried this in hook_views_pre_render and hook_views_pre_execute, both of which have the pager populated and reporting 10 before and 5 after the set, using $view->pager->getItemsPerPage(). Other hooks are too early in the flow and have a null pager value.
The amount of records displayed continues to be 10.

Steps to reproduce

1. Create a view with the full pager
2. Keep pagination 4 Items per page(*Keep your own value).
3. Save the view and access the page.
4. In custom alter view alter (hook_views_post_execute() or hook_views_pre_render()) update the item per page value.
$view->pager->setItemsPerPage(2); (*Keep your own value).
5. Clear the cache and access the view page.

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

💬 Support request
Status

Closed: works as designed

Version

11.0 🔥

Component
Views 

Last updated about 2 hours ago

Created by

🇺🇸United States j. ayen green

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 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 calling setItemsPerPage 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() or hook_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 using hook_views_query_alter(), the pager successfully gets updated.

    Thanks.

  • Status changed to Closed: works as designed 2 months ago
  • 🇳🇱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?

Production build 0.71.5 2024