Show More button does not display if results total less than the Items to Display number

Created on 11 April 2024, 9 months ago

Problem/Motivation

If the number of results in a View is less than the number set in the Items to Display setting then the Show More button does not show at all.

Steps to reproduce

  1. Create a view that has 20 results
  2. In the View, set the Initial items value to 5.
  3. In the View, set the Items to display value to 20.
  4. Visit the Views-generated page.

The show more button will not appear at all.

Remaining tasks

  1. Identify the source of the bug
  2. Write a patch fixing the bug
  3. Test
πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO

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

Comments & Activities

  • Issue created by @cedewey
  • πŸ‡―πŸ‡΄Jordan mohammad-fayoumi Amman

    I'm facing the same exact issue, I'll try to identify the source of the bug

  • πŸ‡―πŸ‡΄Jordan mohammad-fayoumi Amman

    I've changed the pager ID to 2 in order to apply the condition in the following code:

    function views_show_more_preprocess_views_show_more_pager(&$vars) {
      /** @var \Drupal\Core\Pager\PagerManagerInterface $pager_manager */
      $pager_manager = \Drupal::service('pager.manager');
    
      $element = $vars['element'];
      $parameters = $vars['parameters'];
      $pager = $pager_manager->getPager($element);
      if (!$pager) {
        return;
      }
      $current = $pager->getCurrentPage();
      $total = $pager->getTotalPages();
      $pager_classes = ['js-pager__items', 'pager__items', 'pager-show-more'];
    
      if ($current < ($total - 1)) {
        $options = [
          'query' => $pager_manager->getUpdatedParameters($parameters, $element, $current + 1),
        ];
    
        $vars['item'] = [
          'href' => Url::fromRoute('<none>', [], $options),
          'text' => $vars['options']['show_more_text'],
          'attributes' => [
            'title' => t('Go to next page'),
          ],
        ];
      }
      else {
        $pager_classes[] = 'pager-show-more-empty';
      }
    
      $vars['attributes'] = new Attribute([
        'class' => $pager_classes,
      ]);
    
      // This is based on the entire current query string. We need to ensure
      // cacheability is affected accordingly.
      $vars['#cache']['contexts'][] = 'url.query_args';
    
      $vars['heading_id'] = Html::getUniqueId('pagination-heading');
    }

    The reason I did this is because when printing the values of $current and ($total - 1), they would always display 0. By setting the pager ID to 2, I was able to ensure the condition works as expected.

  • πŸ‡ΊπŸ‡ΈUnited States sonfd Portland, ME

    I believe this issue will only occur when both of the following conditions are met:

    1. Initial items is set to a number lower than Items to display
    2. The total number of results is great than Initial items, but less than Items to display

    And I believe this is a limitation of core's Pager class. Upon initialization, it calculates the total number of pages based only on the items per page, it does not consider a different initial amount visible. See: Pager:setTotalPages() which is called from the constructor.

    This is a shame, I would really like to show X initially, then show all after clicking "Show more"

Production build 0.71.5 2024