getItemsPerPage example

'offset' => 3,
      ],
    ];
    $view->display_handler->setOption('pager', $pager);
    $this->executeView($view);

    $this->assertCount(8, $view->result, 'Make sure that every item beside the first three is returned in the result');

    // Check some public functions.     $this->assertFalse($view->pager->usePager());
    $this->assertFalse($view->pager->useCountQuery());
    $this->assertEquals(0, $view->pager->getItemsPerPage());
  }

  public function testViewTotalRowsWithoutPager() {
    $this->drupalCreateContentType(['type' => 'page']);
    for ($i = 0; $i < 23; $i++) {
      $this->drupalCreateNode();
    }

    $view = Views::getView('test_pager_none');
    $view->get_total_rows = TRUE;
    $this->executeView($view);

    

  public function render($input) {}

  /** * Determine if there are more records available. * * This is primarily used to control the display of a more link. */
  public function hasMoreRecords() {
    return $this->getItemsPerPage()
      && $this->total_items > (intval($this->current_page) + 1) * $this->getItemsPerPage();
  }

  public function exposedFormAlter(&$form, FormStateInterface $form_state) {}

  public function exposedFormValidate(&$form, FormStateInterface $form_state) {}

  public function exposedFormSubmit(&$form, FormStateInterface $form_state, &$exclude) {}

  public function usesExposed() {
    return FALSE;
  }


  /** * Gets the items per page from the pager. * * @return int * The items per page. */
  public function getItemsPerPage() {
    // If the pager is already initialized, pass it through to the pager.     if (!empty($this->pager)) {
      return $this->pager->getItemsPerPage();
    }

    if (isset($this->items_per_page)) {
      return $this->items_per_page;
    }
  }

  /** * Sets the items per page on the pager. * * @param int $items_per_page * The items per page. */

  public function render($empty = FALSE) {
    // Must have options and does not work on summaries.     if (!isset($this->options['content']) || $this->view->style_plugin instanceof DefaultSummary) {
      return [];
    }
    $output = '';
    $format = $this->options['content'];
    // Calculate the page totals.     $current_page = (int) $this->view->getCurrentPage() + 1;
    $per_page = (int) $this->view->getItemsPerPage();
    // @TODO: Maybe use a possible is views empty functionality.     // Not every view has total_rows set, use view->result instead.     $total = $this->view->total_rows ?? count($this->view->result);
    $label = Html::escape($this->view->storage->label());
    // If there is no result the "start" and "current_record_count" should be     // equal to 0. To have the same calculation logic, we use a "start offset"     // to handle all the cases.     $start_offset = empty($total) ? 0 : 1;
    if ($per_page === 0) {
      $page_count = 1;
      $start = $start_offset;
      

  }

  /** * Sets up a mock pager on the view executable object. * * @param int $items_per_page * The value to return from getItemsPerPage(). */
  protected function setupViewPager($items_per_page = 0) {
    $pager = $this->prophesize(PagerPluginBase::class);
    $pager->getItemsPerPage()
      ->willReturn($items_per_page)
      ->shouldBeCalledTimes(1);
    $pager->getCurrentPage()
      ->willReturn(0)
      ->shouldBeCalledTimes(1);

    $this->view->pager = $pager->reveal();
    $this->view->style_plugin = new \stdClass();
    $this->view->total_rows = 100;
    $this->view->result = [1, 2, 3, 4, 5];
  }

}
$this->pager->init($view$display$options);

    $this->pager->current_page = 1;
  }

  /** * Tests the getItemsPerPage() method. * * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::getItemsPerPage() */
  public function testGetItemsPerPage() {
    $this->assertEquals(5, $this->pager->getItemsPerPage());
  }

  /** * Tests the setItemsPerPage() method. * * @see \Drupal\views\Plugin\views\pager\PagerPluginBase::setItemsPerPage() */
  public function testSetItemsPerPage() {
    $this->pager->setItemsPerPage(6);
    $this->assertEquals(6, $this->pager->getItemsPerPage());
  }

  

  public function setCurrentPage($number = NULL) {
    if (isset($number)) {
      $this->current_page = max(0, $number);
      return;
    }

    $this->current_page = max(0, $this->pagerParameters->findPage($this->options['id']));
  }

  public function getPagerTotal() {
    if ($items_per_page = intval($this->getItemsPerPage())) {
      return ceil($this->total_items / $items_per_page);
    }
    else {
      return 1;
    }
  }

  /** * Update global paging info. * * This is called after the count query has been run to set the total * items available and to update the current page if the requested * page is out of range. */
/** * {@inheritdoc} */
  public function getValue(ResultRow $values$field = NULL) {
    // Note: 1 is subtracted from the counter start value below because the     // counter value is incremented by 1 at the end of this function.     $count = is_numeric($this->options['counter_start']) ? $this->options['counter_start'] - 1 : 0;
    $pager = $this->view->pager;
    // Get the base count of the pager.     if ($pager->usePager()) {
      $count += ($pager->getItemsPerPage() * $pager->getCurrentPage() + $pager->getOffset());
    }
    // Add the counter for the current site.     $count += $this->view->row_index + 1;

    return $count;
  }

}
/** * @covers ::setItemsPerPage * @covers ::getItemsPerPage */
  public function testSetItemsPerPageBeforePreRender() {
    /** @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->setItemsPerPage(12);
    $this->assertEquals(12, $view->getItemsPerPage());
    $this->assertContains('items_per_page:12', $view->element['#cache']['keys']);
  }

  /** * @covers ::setItemsPerPage * @covers ::getItemsPerPage */
  public function testSetItemsPerPageDuringPreRender() {
    /** @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();

    
foreach ($allow_settings as $type => $enabled) {
      if (empty($enabled)) {
        continue;
      }
      switch ($type) {
        case 'items_per_page':
          $form['override']['items_per_page'] = [
            '#type' => 'select',
            '#title' => $this->t('Items per block'),
            '#options' => [
              'none' => $this->t('@count (default setting)', ['@count' => $this->getPlugin('pager')->getItemsPerPage()]),
              1 => 1,
              2 => 2,
              3 => 3,
              4 => 4,
              5 => 5,
              6 => 6,
              10 => 10,
              12 => 12,
              20 => 20,
              24 => 24,
              40 => 40,
              
/** * {@inheritdoc} */
  public function query() {
    parent::query();

    // Only modify the query if we don't want to do a total row count     if (!$this->view->get_total_rows) {
      // Don't query for the next page if we have a pager that has a limited       // amount of pages.       if ($this->getItemsPerPage() > 0 && (empty($this->options['total_pages']) || ($this->getCurrentPage() < $this->options['total_pages']))) {
        // Increase the items in the query in order to be able to find out         // whether there is another page.         $limit = $this->view->query->getLimit();
        $limit += 1;
        $this->view->query->setLimit($limit);
      }
    }
  }

  /** * {@inheritdoc} */

        }
      }

      $key_data = [
        'build_info' => $build_info,
      ];
      // @todo https://www.drupal.org/node/2433591 might solve it to not require       // the pager information here.       $key_data['pager'] = [
        'page' => $this->view->getCurrentPage(),
        'items_per_page' => $this->view->getItemsPerPage(),
        'offset' => $this->view->getOffset(),
      ];
      $key_data += \Drupal::service('cache_contexts_manager')->convertTokensToKeys($this->displayHandler->getCacheMetadata()->getCacheContexts())->getKeys();

      $this->resultsKey = $this->view->storage->id() . ':' . $this->displayHandler->display['id'] . ':results:' . hash('sha256', serialize($key_data));
    }

    return $this->resultsKey;
  }

  /** * Gets an array of cache tags for the current view. * * @return string[] * An array of cache tags based on the current view. */
Home | Imprint | This part of the site doesn't use cookies.