setCurrentPage example

'view_path' => 1,
      'view_dom_id' => 1,
      'pager_element' => 1,
      'view_base_path' => 1,
      AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER => 1,
      FormBuilderInterface::AJAX_FORM_REQUEST => 1,
      MainContentViewSubscriber::WRAPPER_FORMAT => 1,
    ]));
    $this->container->set('request_stack', $request_stack);
    $view->destroy();
    $view->setDisplay('page_1');
    $view->setCurrentPage(2);
    $this->executeView($view[1]);
    $this->assertSame('<a href="/page_1/1?name=John&amp;sort_by=created&amp;sort_order=ASC&amp;keep=keep&amp;keep_another=1&amp;page=1" class="views-display-link views-display-link-page_1 is-active">Page 1</a>', $this->renderDisplayLink($view, 'display_link_1'));
    $this->assertSame('<a href="/page_2/1?name=John&amp;sort_by=created&amp;sort_order=ASC&amp;keep=keep&amp;keep_another=1&amp;page=1" class="views-display-link views-display-link-page_2">Page 2</a>', $this->renderDisplayLink($view, 'display_link_2'));

    // Assert the validation adds warning messages when a display link is added     // to a display with different filter criteria, sort criteria, pager     // settings or contextual filters. Since all options are added to the     // default display there currently should be no warning messages.     $this->assertNoWarningMessages($view);

    // Assert the message are shown when changing the filter criteria of page_1.
$view->display_handler->overrideOption('cache', [
      'type' => 'time',
      'options' => [
        'results_lifespan' => '3600',
        'output_lifespan' => '3600',
      ],
    ]);

    $mapping = ['views_test_data_name' => 'name'];

    $view->setDisplay('page_1');
    $view->setCurrentPage(0);
    $this->executeView($view);
    $this->assertIdenticalResultset($view[['name' => 'John']['name' => 'George']]$mapping);
    $view->destroy();

    $view->setDisplay('page_1');
    $view->setCurrentPage(1);
    $this->executeView($view);
    $this->assertIdenticalResultset($view[['name' => 'Ringo']['name' => 'Paul']]$mapping);
    $view->destroy();

    $view->setDisplay('page_1');
    

  public function testCounterSecondPage($i) {
    $offset = 3;
    // Setup a pager on the second page.     $this->pager->setOffset($offset);
    $items_per_page = 5;
    $this->pager->setItemsPerPage($items_per_page);
    $current_page = 1;
    $this->pager->setCurrentPage($current_page);

    $rand_start = rand(5, 10);
    $counter_handler = new Counter([], 'counter', $this->definition);
    $options = [
      'counter_start' => $rand_start,
    ];
    $counter_handler->init($this->view, $this->display, $options);

    $this->view->row_index = $i;
    $expected = $items_per_page + $offset + $rand_start + $i;

    
    $this->assertSame(9, $total_error_count);
    $this->assertCount(3, $errors);
  }

  /** * Tests serialization of the ViewExecutable object. */
  public function testSerialization() {
    $view = Views::getView('test_executable_displays');
    $view->setDisplay('page_1');
    $view->setArguments(['test']);
    $view->setCurrentPage(2);

    $serialized = serialize($view);

    // Test the view storage object is not present in the actual serialized     // string.     $this->assertStringNotContainsString('"Drupal\views\Entity\View"', $serialized, 'The Drupal\views\Entity\View class was not found in the serialized string.');

    /** @var \Drupal\views\ViewExecutable $unserialized */
    $unserialized = unserialize($serialized);

    $this->assertInstanceOf(ViewExecutable::class$unserialized);
    
$rand_number = rand(1, 5);
    $view->setItemsPerPage($rand_number);
    $this->assertEquals($rand_number$view->getItemsPerPage(), 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');

    $this->assertNull($view->getOffset(), 'If the pager is not initialized and no manual override there is no offset.');
    $rand_number = rand(1, 5);
    $view->setOffset($rand_number);
    $this->assertEquals($rand_number$view->getOffset(), 'Make sure getOffset uses the settings of setOffset.');

    $this->assertNull($view->getCurrentPage(), 'If the pager is not initialized and no manual override there is no current page.');
    $rand_number = rand(1, 5);
    $view->setCurrentPage($rand_number);
    $this->assertEquals($rand_number$view->getCurrentPage(), 'Make sure getCurrentPage uses the settings of set_current_page.');

    $view->destroy();

    // On this round enable the pager.     $view->initDisplay();
    $view->initQuery();
    $view->initPager();

    $this->assertEquals(5, $view->getItemsPerPage(), 'Per default the view has 5 items per page.');
    $rand_number = rand(1, 5);
    
$this->current_page = $page;

    // Calls like ::unserialize() might call this method without a proper $page.     // Also check whether the element is pre rendered. At that point, the cache     // keys cannot longer be manipulated.     if ($page !== NULL && empty($this->element['#pre_rendered'])) {
      $this->element['#cache']['keys'][] = 'page:' . $page;
    }

    // If the pager is already initialized, pass it through to the pager.     if (!empty($this->pager)) {
      return $this->pager->setCurrentPage($page);
    }
  }

  /** * Gets the current page from the pager. * * @return int * The current page. */
  public function getCurrentPage() {
    // If the pager is already initialized, pass it through to the pager.

  public function testGetCurrentPage() {
    $this->assertEquals(1, $this->pager->getCurrentPage());
  }

  /** * Tests the setCurrentPage() method. * * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setCurrentPage() */
  public function testSetCurrentPage() {
    $this->pager->setCurrentPage(2);
    $this->assertEquals(2, $this->pager->getCurrentPage());

    // A non numeric number or number below 0 should return 0.     $this->pager->setCurrentPage('two');
    $this->assertEquals(0, $this->pager->getCurrentPage());

    $this->pager->setCurrentPage(-2);
    $this->assertEquals(0, $this->pager->getCurrentPage());
  }

  /** * Tests the getTotalItems() method. * * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getTotalItems() */
        return FALSE;

      case 'results':
        // Values to set: $view->result, $view->total_rows, $view->execute_time,         // $view->current_page.         if ($cache = \Drupal::cache($this->resultsBin)->get($this->generateResultsKey())) {
          if (!$cutoff || $cache->created > $cutoff) {
            $this->view->result = $cache->data['result'];
            // Load entities for each result.             $this->view->query->loadEntities($this->view->result);
            $this->view->total_rows = $cache->data['total_rows'];
            $this->view->setCurrentPage($cache->data['current_page'], TRUE);
            $this->view->execute_time = 0;
            return TRUE;
          }
        }
        return FALSE;
    }
  }

  /** * Clear out cached data for a view. */
  
if (($this->options['total_pages'] * $this->options['items_per_page']) < $this->total_items) {
        $this->total_items = $this->options['total_pages'] * $this->options['items_per_page'];
      }
    }

    // Don't set pager settings for items per page = 0.     $items_per_page = $this->getItemsPerPage();
    if (!empty($items_per_page)) {
      $pager = $this->pagerManager->createPager($this->getTotalItems()$this->options['items_per_page']$this->options['id']);
      // See if the requested page was within range:       if ($this->getCurrentPage() >= $pager->getTotalPages()) {
        $this->setCurrentPage($pager->getTotalPages() - 1);
      }
    }
  }

  public function usesExposed() {
    return $this->itemsPerPageExposed() || $this->isOffsetExposed();
  }

  protected function itemsPerPageExposed() {
    return !empty($this->options['expose']['items_per_page']);
  }

  
'type' => 'mini',
      'options' => ['items_per_page' => 1],
    ]);

    $view->preview();

    $counter = $view->style_plugin->getField(0, 'counter');
    $this->assertEquals('1', $counter);
    $view->destroy();

    // Go to the second page.     $view->setCurrentPage(1);
    $view->preview();

    $counter = $view->style_plugin->getField(0, 'counter');
    $this->assertEquals('2', $counter);
    $view->destroy();

    // Go to the third page.     $view->setCurrentPage(2);
    $view->preview();

    $counter = $view->style_plugin->getField(0, 'counter');
    

  public function __construct($totalItems$limit$currentPage = 0) {
    $this->totalItems = $totalItems;
    $this->limit = $limit;
    $this->setTotalPages($totalItems$limit);
    $this->setCurrentPage($currentPage);
  }

  /** * Sets the current page to a valid value within range. * * If a page that does not correspond to the actual range of the result set * was provided, this function will set the closest page actually within * the result set. * * @param int $currentPage * (optional) The current page. */
    // Page 1.     \Drupal::request()->query->set('page', 0);
    $tags_page_1 = Cache::mergeTags($base_tags$entities[1]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1$entities[2]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1$entities[3]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1$entities[4]->getCacheTags());
    $tags_page_1 = Cache::mergeTags($tags_page_1$entities[5]->getCacheTags());
    $this->assertViewsCacheTags($view$tags_page_1$do_assert_views_caches$tags_page_1);
    $this->assertViewsCacheTagsFromStaticRenderArray($view$tags_page_1$do_assert_views_caches);
    $view->destroy();
    // Page 2.     $view->setCurrentPage(1);
    \Drupal::request()->query->set('page', 1);
    $tags_page_2 = Cache::mergeTags($base_tags$entities[0]->getCacheTags());
    $this->assertViewsCacheTags($view$tags_page_2$do_assert_views_caches$tags_page_2);
    $view->destroy();

    // Ensure that invalidation works on both pages.     // Page 2.     $view->setCurrentPage(1);
    \Drupal::request()->query->set('page', 1);
    $entities[0]->name->value = $random_name = $this->randomMachineName();
    $entities[0]->save();
    
$not_expected_nids[] = $node->id();

    $column_map = ['nid' => 'nid'];

    $view->setDisplay('page_1');
    $this->executeView($view);
    $this->assertIdenticalResultset($viewarray_slice($expected, 0, 10)$column_map, 'Ensure that the right nodes are displayed on the frontpage.');
    $this->assertNotInResultSet($view$not_expected_nids, 'Ensure no unexpected node is in the result.');
    $view->destroy();

    $view->setDisplay('page_1');
    $view->setCurrentPage(1);
    $this->executeView($view);
    $this->assertIdenticalResultset($viewarray_slice($expected, 10, 10)$column_map, 'Ensure that the right nodes are displayed on second page of the frontpage.');
    $this->assertNotInResultSet($view$not_expected_nids, 'Ensure no unexpected node is in the result.');
    $view->destroy();
  }

  /** * Verifies that an amount of nids aren't in the result. * * @param \Drupal\views\ViewExecutable $view * An executed View. * @param array $not_expected_nids * An array of nids which should not be part of the resultset. * @param string $message * (optional) A custom message to display with the assertion. * * @internal */


  /** * @covers ::setCurrentPage * @covers ::getCurrentPage */
  public function testSetCurrentPageBeforePreRender() {
    /** @var \Drupal\views\ViewExecutable|\PHPUnit\Framework\MockObject\MockObject $view */
    /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit\Framework\MockObject\MockObject $display */
    [$view$display] = $this->setupBaseViewAndDisplay();

    $view->setCurrentPage(12);
    $this->assertEquals(12, $view->getCurrentPage());
    $this->assertContains('page:12', $view->element['#cache']['keys']);
  }

  /** * @covers ::setCurrentPage * @covers ::getCurrentPage */
  public function testSetCurrentPageDuringPreRender() {
    /** @var \Drupal\views\ViewExecutable|\PHPUnit\Framework\MockObject\MockObject $view */
    /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit\Framework\MockObject\MockObject $display */
    [
Home | Imprint | This part of the site doesn't use cookies.