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;