I'm using Drupal 8.0.1. I'm starting to develop a new custom module for learning purpose. Just I have created one form. From the form, the values are stored in my custom table. Now I want to show the records in separate page.

In drupal 7, I have used

  $query = db_query('SELECT id,name,email,age,gender FROM {learning}');
  $rows = array();
  while ($data = $query->fetchAssoc()) {
      // Fill the table rows
    $data['operation'] = l(t('Delete'), 'admin/people/survey/' . $data['id'] . '/delete');
    $data['operation'] .= "|";
    $data['operation'] .= l(t('Edit'), 'admin/people/survey/' . $data['id'] . '/edit');
    $rows[] = array('data' => $data , 'style' => 'word-break:break-all;' , );
  //Number of records shown in per page
  $per_page = 4;
  $current_page = pager_default_initialize(count($rows), $per_page);
  $chunks = array_chunk($rows, $per_page, TRUE);
  // Prepare table header
  $header = array(t('ID'), t('Name'), t('Email'), t('Age'), t('Gender'), t('Operations'));
  // Output of table with the paging
  $output = theme('table',
      "header" => $header,
      "rows" => $chunks[$current_page],
      "attributes" => array(),
      "sticky" => TRUE,
      "caption" => "",
      "colgroups" => array(),
      "empty" => t("No Survey Records has been found")
  //return pager with limited number of records.
  return $output .= theme('pager', array('quantity', count($rows)));;

And this was working fine in drupal 7. I just want to do same in the Drupal 8. When I put this code in drupal 8 it shows error theme() not found. How can I create the table to show the list of records from my custom table.


2 Answers 2


In order to achieve this you need to do the following

  • Inject the database service as db_query is not working in Drupal 8 anymore
  • Use of theme() was discouraged in Drupal 7 as well, instead you need to use render array #theme key
  • Use Drupal 8 Database API db_select and chaining stuff and extend a special class Drupal\Core\Database\Query\TableSortExtender for rendering the data in tabular form
  • Finally form a render array with #theme key, #header, #rows.

The following example from my custom module display the data in table along with pager

      // Prepare _sortable_ table header
      $header = array(
        array('data' => t('ID'), 'field' => 'id'),
        array('data' => t('Name'), 'field' => 'name'),
        array('data' => t('Email'), 'field' => 'field1'),
        array('data' => t('Age'), 'field' => 'field2', 'sort' => 'desc'),
        array('data' => t('Gender'), 'field' => 'field3'),
        array('data' => t('Operations'), 'field' => 'field4'),

      $query = $this->database->select('location','loc');
      $query->fields('loc', array('id', 'name', 'field1','field2','field3','field4'));
      $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
      $pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);
      $result = $pager->execute();

      foreach($result as $row) {
          $rows[] = array('data' => (array) $row);

      $build = array(
          '#markup' => t('List of All locations')

      $build['location_table'] = array(
        '#theme' => 'table', '#header' => $header,
          '#rows' => $rows
     $build['pager'] = array(
       '#type' => 'pager'

Its not a simple question, there is couple of things that you need to change for your module working properly in Drupal 8. Remember Drupal 8 now uses OOP and achitecture is modernized. Here you can check a nice presentation about this.

Here you can check a bunch of module examples, look at one called 'dbtng_example' this is exactly that you want.

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.