-1

I'm currently new with Drupal 8 and right now I'm trying to output a pagination on my twig file using entity query. So far this is my current code that I came up with. My goal is output the news content type using entity and add a pagination. I've already added pager(9) to display only 9 items. I tried inserting {{pager}} on my twig file but I don't see any pagination being rendered. I'm not sure what I'm missing here. Really need some help.

// Entity Query for News
//////////////////////////////////////////////////


$newsPostings = $query->get('node')
    ->condition('status', 1, '=')
    ->condition('type', 'article')
    ->sort('created', 'desc')
    ->pager(9)
    ->execute();

foreach ($newsPostings as $key => $news_postings) {

    $newsNode = _nodeLoad($news_postings);


    $publishDate = strtotime($newsNode->get('created')->value);
    $articleCreated = date('F j, Y', $publishDate);


    $variables['article'][$key]['title'] = $newsNode->get('title')->value;
    $variables['article'][$key]['image'] = $newsNode->field_image->entity->url();
    $variables['article'][$key]['path'] = $newsNode->toUrl();
    $variables['article'][$key]['date'] = $articleCreated;
}

// Pager for News Entity Query
//////////////////////////////////////////////////
$nodesNews =  \Drupal\node\Entity\Node::loadMultiple($newsPostings);
$pathNews = base_path();

$pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(9);
$result = $pager->execute();

 $build['results'] = array(
     '#theme' => 'page__news',
     '#items' => $nodesNews,
     '#path'  => $pathNews,
   );

   $build['pager'] = array(
     '#type' => 'pager'
   );


return $build;

2 Answers 2

0

Modify your query to add pager,for the pagination we need to extend the pagerselectextender and add limit in the query.

 $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(9);
 $result = $pager->execute();


/////////////////updated////////////////////
     $query->get('node')
     $query->condition('status', 1, '=')
     $query->condition('type', 'article')
     $query->sort('created', 'desc')
     $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(1);
     $newsPostings = $pager->execute();
////////////////////updated///////////////////////////


  $build['results'] = array(
      '#theme' => 'page__news',
      '#items' => $nodesNews,
      '#path'  => $pathNews,
    );

    $build['pager'] = array(
      '#type' => 'pager'
    );
9
  • I think I've tried this before, tried it again it comes out empty :(
    – user77193
    Oct 10, 2017 at 4:14
  • Really not sure which part I'm currently getting it wrong :(
    – user77193
    Oct 10, 2017 at 4:18
  • Right now it gives me this error Call to undefined method Drupal\Core\Entity\Query\QueryFactory::extend() in aim_preprocess_page() basically it is pointing to this line $pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(9);
    – user77193
    Oct 10, 2017 at 4:40
  • Your d8 version? Could you please paste the modified query?
    – developer
    Oct 10, 2017 at 4:48
  • Updated my question with the updated code you requested and yes I'm using Drupal 8
    – user77193
    Oct 10, 2017 at 4:49
0

Use the render service either via dependency injection or directly from service container.

$pager = array(
      '#type' => 'pager'
    );

$build['results'] = array(
      '#theme' => 'page__news',
      '#items' => $nodesNews,
      '#path'  => $pathNews,
      '#pager' => \Drupal::service('renderer')->render($pager)
    );

Then print {{ pager }} into your twig file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.