0

This question already has an answer here:

I have custom twig template for product listing

Below is my code in controller

$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'product')
->pager(1)
->sort('created', 'DESC');

$nids = $query->execute();
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

pager_default_initialize($query->total, '1');

$list = array();
$path = base_path();
foreach ($nodes AS $node){

    $nid = $node->get('nid')->value;
    $list[$nid]['title'] = $node->get('title')->value;
    $list[$nid]['image'] = str_replace('public://', $base_url . '/sites/default/files/', $node->get('field_product_image')->entity->getFileuri());
    $list[$nid]['desc'] = $node->body->value;
}


$build['result'] = array(
'#theme' => 'list_product',
'#items' => $list,
'#path'  => $path,
   '#pager' => array(
        '#type' => 'pager',
    ),
 );

 return $build;

Here is my twig file code

<ul>
  {% for item in items %}
    <li>
        <img src="{{item.image}}" style="max-width: 100px;height: auto;float:left;" />
        <div style="display: inline-block;lfloat:left;">
        <a href="#">
            <span>{{ item.title }}</span>
        </a>
            <span>{{ item.desc|raw }}</span>
        </div>
    </li>
  {% endfor %}
</ul>

<div>{{ pager }}</div>

Still pager is not getting rendered.

Note: The examples in How to add Pager to a custom page? tell you how you can set pager and return it, I need a solution to render it in my list.html.twig template.

marked as duplicate by Beebee, Community May 14 '18 at 6:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Those examples tell you how you can set pager and return it, I need a solution to render it in my list.html.twig template – Nayana May 11 '18 at 10:45
  • 1
    Issue was $query->total was not getting proper value and that is why pager was not rendering. – Nayana May 14 '18 at 6:41