Problem/Motivation

It would be nice to be able to have "Show X more" as the button text, where X is the number of items in the next page.

Proposed resolution

Similar to \Drupal\views\Plugin\views\area\Result, calculate this number at render time and replace a special token in the button text option.

Comments

acbramley created an issue. See original summary.

acbramley’s picture

Status: Active » Needs review
StatusFileSize
new3.98 KB
sam152’s picture

Status: Needs review » Needs work

Looking good, thanks for the test. Review as follows.

  1. +++ b/src/Plugin/views/pager/InfiniteScroll.php
    @@ -23,6 +23,27 @@ class InfiniteScroll extends SqlBase {
    +    if (!empty($text) && strpos($text, '@') !== FALSE) {
    

    Is @token_name the approach the rest of views uses? I think they faked twig and started using {{ foo }} for their tokens? There might be some views methods that deal with this.

  2. +++ b/src/Plugin/views/pager/InfiniteScroll.php
    @@ -23,6 +23,27 @@ class InfiniteScroll extends SqlBase {
    +      $items_per_page = (int) $this->view->getItemsPerPage();
    +      $total = (int) $this->getTotalItems();
    +      $current_page = (int) $this->getCurrentPage() + 1;
    +
    +      // Default to the pager amount.
    +      $next_page_count = $items_per_page;
    +      // Calculate the remaining items if we are at the 2nd to last page.
    +      if ($current_page >= ceil($total / $items_per_page) - 1) {
    +        $next_page_count = $total - ($current_page * $items_per_page);
    +      }
    +
    

    Lets move everything that calculates the number of items left into a getNumberItemsLeft method. Almost a good candidate for a unit test.

  3. +++ b/src/Plugin/views/pager/InfiniteScroll.php
    @@ -23,6 +23,27 @@ class InfiniteScroll extends SqlBase {
    +      $replacements = [
    +        '@next_page_count' => $next_page_count,
    +      ];
    +      $text = str_replace(array_keys($replacements), array_values($replacements), $text);
    

    strtr will do key/value replacements without array_keys/array_values.

  4. +++ b/src/Plugin/views/pager/InfiniteScroll.php
    @@ -72,6 +93,14 @@ class InfiniteScroll extends SqlBase {
    +    $item_list = [
    +      '#theme' => 'item_list',
    +      '#items' => [
    +        '@next_page_count -- the next page record count',
    +      ],
    +    ];
    +
    +    $list = $this->getRenderer()->render($item_list);
    
    @@ -84,6 +113,7 @@ class InfiniteScroll extends SqlBase {
    +        '#description' => $this->t('The following tokens are supported:') . $list,
    

    We can probably use #prefix/#suffix on the list to get a description in there, instead of rendering early.

  5. +++ b/tests/src/FunctionalJavascript/InfiniteScrollTest.php
    @@ -74,6 +74,16 @@ class InfiniteScrollTest extends JavascriptTestBase {
    +    // Test @next_page_count token.
    +    $this->createView('next-page-count', [
    +      'button_text' => 'Load @next_page_count more',
    +      'automatically_load_content' => FALSE,
    +    ], 6);
    +    $this->drupalGet('next-page-count');
    +    $this->getSession()->getPage()->clickLink('Load 5 more');
    +    $this->getSession()->wait(static::ajaxWaitDelay);
    +    $this->assertTotalNodes(11);
    

    <3

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new3.79 KB
new3.3 KB

Thanks for the review!
1. Admittedly I'm not too sure, I was going off what the Result area plugin does. Happy to switch it up though.
2. Good idea, that was lazy of me!
3. Ah ofcourse, done!
4. Good idea!
5. :)

fenstrat’s picture

StatusFileSize
new3.76 KB

Straight reroll of #4 as it no longer applied.

hugronaphor’s picture

StatusFileSize
new3.91 KB
new1.91 KB

My use-case is to have a string as: Load More @next_page_count more tickets (@total tickets total)
So I'm adding @total token as well.

Status: Needs review » Needs work

The last submitted patch, 6: interdiff--5-6.patch, failed testing. View results

hugronaphor’s picture

Status: Needs work » Needs review
sam152’s picture

  • Sam152 committed 74c9ba8 on 8.x-1.x authored by hugronaphor
    Issue #2879588 by acbramley, hugronaphor, fenstrat, Sam152: Provide...
sam152’s picture

Status: Needs review » Fixed

This dropped off the radar. Looks good to go for me.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.