initPager example

$this->assertTrue($view->ajaxEnabled());

    $view->setDisplay();
    // There should be no pager set initially.     $this->assertNull($view->usePager());

    // Add a pager, initialize, and test.     $view->displayHandlers->get('default')->overrideOption('pager', [
      'type' => 'full',
      'options' => ['items_per_page' => 10],
    ]);
    $view->initPager();
    $this->assertTrue($view->usePager());

    // Test setting and getting the offset.     $rand = rand();
    $view->setOffset($rand);
    $this->assertEquals($rand$view->getOffset());

    // Test the getBaseTable() method.     $expected = [
      'views_test_data' => TRUE,
      '#global' => TRUE,
    ];

  }

  /** * Gets the current pager plugin. * * @return \Drupal\views\Plugin\views\pager\PagerPluginBase * The current pager plugin. */
  public function getPager() {
    if (!isset($this->pager)) {
      $this->initPager();
    }

    return $this->pager;
  }

  /** * Initializes the pager. * * Like style initialization, pager initialization is held until late to allow * for overrides. */
  

  public function build(ViewExecutable $view) {
    // Make the query distinct if the option was set.     if (!empty($this->options['distinct'])) {
      $this->setDistinct(TRUE);
    }

    // Store the view in the object to be able to use it later.     $this->view = $view;

    $view->initPager();

    // Let the pager modify the query to add limits.     $view->pager->query();

    $view->build_info['query'] = $this->query();
    $view->build_info['count_query'] = $this->query(TRUE);
  }

  /** * Executes the query and fills associated view object with according values. * * Values to set: $view->result, $view->total_rows, $view->execute_time, * $view->current_page. */
$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);
    $view->setItemsPerPage($rand_number);
    $rand_number = rand(6, 11);
    $view->pager->setItemsPerPage($rand_number);
    $this->assertEquals($rand_number$view->getItemsPerPage(), 'Make sure getItemsPerPage uses the settings of setItemsPerPage.');

    $this->assertEquals(0, $view->getOffset(), 'Per default a view has a 0 offset.');
    $rand_number = rand(1, 5);
    $view->setOffset($rand_number);
    
Home | Imprint | This part of the site doesn't use cookies.