1

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.

MyController.php

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')
           ->tableSort($header)
           ->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',
                'title',               
                'Name',
                'Ip',
                'SLU',
            ];  
        foreach ($nids as $nid) {
            $node = Node::load($nid);
            $row  = [
                'data' => [
                    $node->nid->value,
                    $node->title->value, 
                    $node->field_p_name[0]->value,
                    $node->field_ip[0]->value,
                    $node->field_p_slu[0]->value,
                ],
            ];
            $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.

screenshot

How do I resolve this issue?

2

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
2

Here is an example of sortable table with pagination.

$header = [
  'label' => [
    'data' => $this->t('ID'),
    'specifier' => 'nid',
  ],
  'replies' => [
    'data' => $this->t('Title'),
    'specifier' => 'title',
  ],
  'created' => [
    'data' => $this->t('Created'),
    'specifier' => 'created',
    // Set default sort criteria.
    'sort' => 'desc',
  ],
  'uid' => [
    'data' => $this->t('Author'),
    'specifier' => 'uid',
  ],
];

$storage = \Drupal::entityTypeManager()->getStorage('node');

$query = $storage->getQuery();
$query->condition('status', \Drupal\node\NodeInterface::PUBLISHED);
$query->condition('type', 'article');
$query->tableSort($header);
// Default value is 10.
$query->pager(15);
$nids = $query->execute();

$date_formatter = \Drupal::service('date.formatter');
$rows = [];
foreach ($storage->loadMultiple($nids) as $node) {
  $row = [];
  $row[] = $node->id();
  $row[] = $node->toLink();
  $created = $node->get('created')->value;
  $row[] = [
    'data' => [
      '#theme' => 'time',
      '#text' => $date_formatter->format($created),
      '#attributes' => [
        'datetime' => $date_formatter->format($created, 'custom', \DateTime::RFC3339),
      ],
    ],
  ];
  $row[] = [
    'data' => $node->get('uid')->view(),
  ];
  $rows[] = $row;
}

$build['table'] = [
  '#type' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => $this->t('No content has been found.'),
];

$build['pager'] = [
  '#type' => 'pager',
];

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.