6

How do I display pager using EntityFieldQuery for fetch results? I can't find complete example.

I am doing simmilar to: Sortable, paged table retrieved using EntityFieldQuery

But I miss part to output simple pagination links (1,2,3... for every next page).

8

I suspect the only thing you're missing is adding the return from theme('pager') to the content you're building.

This is a small example that shows teasers for all nodes in the system, with 10 per page. You would use this in a page callback function:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->pager(10);

$results = $query->execute();

$content = node_view_multiple(node_load_multiple(array_keys($results['node'])));

$build = array(
  'content' => $content,
  'pager' => array(
    '#theme' => 'pager',
    '#weight' => 10
  )
);

return $build;

More specific based on your comments:

if ($type == 21) { 
  $query = new EntityFieldQuery(); 

  $entities = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'product') 
    ->fieldCondition('field_razred', 'tid', array (607,608,609,610), 'IN')
    ->pager(10) // this is the bit you're missing
    ->execute(); 

  $nodes = node_load_multiple(array_keys($entities['node'])); 
  print render(node_view_multiple($nodes, 'teaser')) . theme('pager');
}
  • Thanks, it is a good clue. I am not using it in context you suggest, though. I have my query like this: if ($type==21) { $query = new EntityFieldQuery(); $entities = $query->entityCondition('entity_type', 'node') ->propertyCondition('type', 'product') ->fieldCondition('field_razred', 'tid', array (607,608,609,610), 'IN') ->execute(); } $nodes = node_load_multiple(array_keys($entities['node'])); print render(node_view_multiple($nodes, 'teaser')); How would the method you mention apply in my situation ? Thanks a lot! – NenadP Aug 27 '12 at 16:33
  • 1
    Just change the last line to print render(node_view_multiple($nodes, 'teaser')) . theme('pager'); :) – Clive Aug 27 '12 at 16:38
  • changed, but to no effect. :) Please bear with me, im not experienced with theme() ;) Do i need to have something else ? Pager is not shown. – NenadP Aug 27 '12 at 16:52
  • 1
    Sorry I didn't look further up...you need to call the pager() method on the EntityFieldQuery, I've updated the answer with an example, it should work...unless you've got other pagers on the same page in which case you might need to make a couple of changes – Clive Aug 27 '12 at 16:56
  • Yep i did it already (added ->pager(10)) but there is no pager, nor any error. Does theme('pager') depends on something i am not aware of ? I built my theme on Framework theme. This code is in fact run in page.tpl.php (when conditions are met) – NenadP Aug 27 '12 at 17:04
4

Also been having some trouble with this - debugging statements initializePager showed that the optional $element parameter was being set to 1 rather than 0.

If there is only one pager being displayed, try setting it explicitly to 0. $query->pager(10, 0);

  • Never had luck with this. I ended up in writing my own pager functions/markup! Will check if this helps. Thanks – NenadP May 23 '13 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.