7.x entity.inc EntityFieldQuery::pager($limit = 10, $element = NULL)

Enables a pager for the query.

Parameters

$limit: An integer specifying the number of elements per page. If passed a false value (FALSE, 0, NULL), the pager is disabled.

$element: An optional integer to distinguish between multiple pagers on one page. If not provided, one is automatically calculated.

Return value

EntityFieldQuery The called object.

File

includes/entity.inc, line 1044

Class

EntityFieldQuery
Retrieves entities matching a given set of conditions.

Code

public function pager($limit = 10, $element = NULL) {
  if (!isset($element)) {
    $element = PagerDefault::$maxElement++;
  }
  elseif ($element >= PagerDefault::$maxElement) {
    PagerDefault::$maxElement = $element + 1;
  }
  $this->pager = array(
    'limit' => $limit,
    'element' => $element,
  );
  return $this;
}

Comments

kieran_cc’s picture

If I pass a limit of 4, the EFQ returns the same 4 nodes everytime because there is no option to pass a page number.
I have tried using $query->initialisePager() but that only works after ->pager() has been called which doesn't make logical sense to me.
I have tried directly altering the page array's start value but that has no effect

kingandy’s picture

The pager system picks up the "current" page from the URL - theme_pager() generates a link with a "page" query string element, which is then used to drive the page selection. You could probably build this link manually, but it's easier (and safer) to just render a standard pager and let Drupal do the heavy lifting.

In most cases something like this will be enough:

  $efq = new EntityFieldQuery();
  // ... Set up your EFQ conditions here ...
  $efq->pager(4);

  // Later, when we are building our output array:
  $output['pager'] = array('#theme' => 'pager');

(You could also use theme('pager'); if you're working in a context where you need to return HTML code directly, rather than a renderable array.)

If you're building a page with more than one pager, and you don't want both pagers to be on the same page, you'll also need to let your query and your pager know which "pager element" to use. Luckily this is fairly straightforward:

  $element = 3;
  $efq = new EntityFieldQuery();
  // ... Set up your EFQ conditions here ...
  $efq->pager(4, $element);

  // Later, when we are building our output array:
  $output['pager'] = array('#theme' => 'pager', '#element' => $element);

(Again, the direct HTML equivalent would be theme('pager', array('element' => $element)); .)