I've created a custom module to get X items using entityQuery, and then display them as a list. I need to limit these and add a pager to click through to the next page, but I cannot find how best to do this. Examples I've found either use external search sources, or are depreciated functions of older versions of Drupal.
I've added the pager option in the query, initialized the pager, and added the #type = pager
to the content block, but it's not showing up. I know I am missing something but I cant figure out what it is.
class MyPageController extends ControllerBase {
public function myPage1() {
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'page')
->range(0, 10)
->pager(3)
->sort('created', 'DESC');
$nids = $query->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);
pager_default_initialize($query->total, '7');
$list = array();
foreach ($nodes AS $node){
$list[] = $node->get('title')->value;
}
$content = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#title' => 'Page list',
'#items' => $list,
'#attributes' => ['class' => 'my_page1_list'],
'#wrapper_attributes' => ['class' => 'container'],
'#type' => ['pager'],
];
return $content;
}
}