- 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:
- Initial items is set to a number lower than Items to display
- 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"