Score:2

I am trying to add a simple pager to my controller, I have managed to get the row limit (5) to be printed but not to see the next or the previous

no flag
<?php

namespace Drupal\drupal_block\Controller;


use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Query\PagerSelectExtender;

class DrupalBlockController extends ControllerBase {
  protected $connection;

  public function __construct(Connection $connection) {
      $this->connection = $connection;
  }

  public static function create(ContainerInterface $container)
  {
      return new static(
          $container ->get('database')
      );
  }


  public function mymodule()
  {
    $data = $this->connection->select('custom_table', 'ct')
      ->fields('ct', ['name', 'lastname', 'sexo', 'email']);


  $pager = $data->extend(PagerSelectExtender::class)->limit(5);
      //$pager->setCurrentPage($page);
      $data = $pager->execute()->fetchAll();
      $rows = array();

      foreach ($data as $row){
        $rows[] = [
          'name' => $row->name,
          'lastname' => $row->lastname,
          'sexo' => $row->sexo,
          'email' => $row->email,
        ];
      }


    return [
      '#theme' => 'table',
      '#pager' => [
        '#type' =>'pager'
      ],
      '#header' => [
        $this->t('Name'),
        $this->t('Last Name'),
        $this->t('Gender'),
        $this->t('Email')
      ],
      '#rows' => $rows,
      ];
      }
    }

enter image description here

Score:1
de flag

This:

return [
  '#theme' => 'table',
  '#pager' => [
    '#type' =>'pager'
  ],
  '#header' => [
    $this->t('Name'),
    $this->t('Last Name'),
    $this->t('Gender'),
    $this->t('Email')
  ],
  '#rows' => $rows,
];

Should be this:

return [
  'pager' => [
    '#type' =>'pager'
  ],
  'table' => [
    '#theme' => 'table',
    '#header' => [
      $this->t('Name'),
      $this->t('Last Name'),
      $this->t('Gender'),
      $this->t('Email')
    ],
    '#rows' => $rows,
  ],
];

  1. Elements with hashtag prefixes are not rendered, they are metadata. So the hashtag needs to be removed from the array key.
  2. The pager needs to be a separate render array from the table.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.