setItemsPerPage example



  /** * Allows to change the display settings right before executing the block. * * @param \Drupal\views\Plugin\Block\ViewsBlock $block * The block plugin for views displays. */
  public function preBlockBuild(ViewsBlock $block) {
    $config = $block->getConfiguration();
    if ($config['items_per_page'] !== 'none') {
      $this->view->setItemsPerPage($config['items_per_page']);
    }
  }

  /** * {@inheritdoc} */
  public function usesExposedFormInBlock() {
    return TRUE;
  }

  /** * {@inheritdoc} */

  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());
  }

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

  

  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;
    
$options['base_path'] = ['default' => ''];
    $options['count'] = ['default' => TRUE];
    $options['override'] = ['default' => FALSE];
    $options['items_per_page'] = ['default' => 25];

    return $options;
  }

  public function query() {
    if (!empty($this->options['override'])) {
      $this->view->setItemsPerPage(intval($this->options['items_per_page']));
    }
  }

  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    $form['base_path'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Base path'),
      '#default_value' => $this->options['base_path'],
      '#description' => $this->t('Define the base path for links in this summary view, i.e. http://example.com/<strong>your_view_path/archive</strong>. Do not include beginning and ending forward slash. If this value is empty, views will use the first path found as the base path, in page displays, or / if no path could be found.'),

  public static $testViews = ['test_tokens', 'test_invalid_tokens'];

  /** * Tests core token replacements generated from a view. */
  public function testTokenReplacement() {
    $token_handler = \Drupal::token();
    $view = Views::getView('test_tokens');
    $view->setDisplay('page_1');
    // Force the view to span more than one page to better test page_count.     $view->display_handler->getPlugin('pager')->setItemsPerPage(4);
    $this->executeView($view);

    $expected = [
      '[view:label]' => 'Test tokens',
      '[view:description]' => 'Test view to token replacement tests.',
      '[view:id]' => 'test_tokens',
      '[view:title]' => 'Test token page',
      '[view:url]' => $view->getUrl(NULL, 'page_1')->setAbsolute(TRUE)->toString(),
      '[view:total-rows]' => '5',
      '[view:base-table]' => 'views_test_data',
      '[view:base-field]' => 'id',
      

class None extends PagerPluginBase {

  /** * {@inheritdoc} */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view$display$options);

    // If the pager is set to none, then it should show all items.     $this->setItemsPerPage(0);
  }

  public function summaryTitle() {
    if (!empty($this->options['offset'])) {
      return $this->t('All items, skip @skip', ['@skip' => $this->options['offset']]);
    }
    return $this->t('All items');
  }

  protected function defineOptions() {
    $options = parent::defineOptions();
    


  /** * @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 */
    [

  public function setItemsPerPage($items_per_page) {
    // Check whether the element is pre rendered. At that point, the cache keys     // cannot longer be manipulated.     if (empty($this->element['#pre_rendered'])) {
      $this->element['#cache']['keys'][] = 'items_per_page:' . $items_per_page;
    }
    $this->items_per_page = $items_per_page;

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

  /** * Gets the pager offset from the pager. * * @return int * The pager offset. */
  public function getOffset() {
    // If the pager is already initialized, pass it through to the pager.
/** * Tests the api functions on the view object. */
  public function testPagerApi() {
    $view = Views::getView('test_pager_full');
    $view->setDisplay();
    // On the first round don't initialize the pager.
    $this->assertNull($view->getItemsPerPage(), 'If the pager is not initialized and no manual override there is no items per page.');
    $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.');

    

      }

      $this->view->query->addWhere(0, $conditions);
    }

    // Add an IN condition for validation.     if (!empty($options['ids'])) {
      $this->view->query->addWhere(0, $id_table . '.' . $id_field$options['ids'], 'IN');
    }

    $this->view->setItemsPerPage($options['limit']);
  }

  /** * {@inheritdoc} */
  public function validate() {
    $errors = parent::validate();
    // Verify that search fields are set up.     $style = $this->getOption('style');
    if (!isset($style['options']['search_fields'])) {
      $errors[] = $this->t('Display "@display" needs a selected search fields to work properly. See the settings for the Entity Reference list format.', ['@display' => $this->display['display_title']]);
    }
Home | Imprint | This part of the site doesn't use cookies.