I have a function in a controller which shows data in table format. I need pagination and styling for the table. I tried as follow.


public function my_requests() {       
        //load the current user details
        $user= User::load(\Drupal::currentUser()->id());
        $user_id = \Drupal::currentUser()->id();

        //Query the database
        $query = \Drupal::entityQuery('node')
           ->condition('status', 1)
           ->condition('type', "forward_proxy_request")
           ->condition('uid', $user_id)
           ->sort('nid' , 'ASC');
        $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
        $pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);
        $nids = $pager->execute();

        //creates the table header
        $header = [
                'Node id',
        foreach ($nids as $nid) {
            $node = Node::load($nid);
            $row  = [
                'data' => [
            $rows[] = $row;
        //Build the table
        $build = [
          'table'           => [
          '#prefix'        => '<h1>My Requests</h1>',
          '#theme'         => 'table',
          '#attributes'    => [
              'data-striping' => 0
          '#header' => $header,
          '#rows'   => $rows,
    //for pagination
    $build['pager'] = array(
        '#type' => 'pager'

     return $build;          

But it throws an error.

Error: Call to undefined method Drupal\\Core\\Entity\\Query\\Sql\\Query::extend() in /var/www-drupal/modules/ga/src/Controller/MyController.php on line 196

Without extendQuery, it shows a plain table without any style.


How do I resolve this issue?


Query extenders is a concept of low-level database queries. Content entities don't have that. The extenders just add the ability to do something, they don't do anything out of the box.

Both the pager and table sort functionality is built-in in entity queries. Just call pager() and tableSort() as you would do with a raw query after adding the extenders.

  • Hows the working of tableSort() ? How can i add ASC and DESC buttons to table headers? Pager is working well . – Crazyrubixfan Mar 26 '18 at 14:33
  • hi how can add sorting facility to headers(with up and down arrows) like in data tables ? – Crazyrubixfan Mar 27 '18 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.