It is simple to display record/data from database in drupal 8 with pagination, here we are going to explain by example with some simple steps:
Step 1:
Create a folder with name of "gpcustom", this is our custom module name. Now create a file "gpcustom.info.yml" for create identity of module.
and the code for this file is..
name: Student Records
description: 'Display student records'
type: module
core: 8.x
Step 2:
Create a file with name of "gpcustom.routing.yml", here we are going to declare routing means path for display record page.
and the code for this file is..
google_feeds_list:
path: 'student/record'
defaults:
_controller: '\Drupal\gpcustom\Controller\gpcustomController::student_record'
_title: 'Student Records'
requirements:
_permission: 'access content'
// _controller: '\Drupal\gpcustom\Controller\gpcustomController::student_record' means '\Drupal\gpcustom\Controller\viewController' is file path for function and 'student_record' is the function name.
// _permission: 'access content' means anybody can access this page without login.
// path: 'student/record' means display record page url.
// _title: 'Student Records' means title of page.
Step 3:
Create a folder with this hierarchy '\src\Controller\' and file with name of "gpcustomController.php".
and the code for this file is..
<?php
namespace Drupal\gpcustom\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
/**
* Class DisplayTableController.
*
* @package Drupal\gpcustom\Controller
*/
class gpcustomController extends ControllerBase {
/**
* Returns a render-able array for a test page.
*/
public function student_record() {
//create table header
$header = array(
'id'=> t('SrNo'),
'name' => t('Candidate Name'),
'mobile' => t('Mobile Number'),
'email' => t('Email ID'),
'age' => t('Age'),
'gender' => t('Gender'),
'opt' => t('operations'),
);
//select records from table
$query = \Drupal::database()->select('mydata', 'm');
$query->fields('m', ['id','name','mobilenumber','email','age','gender']);
$pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(3);
// The actual action of sorting the rows is here.
$table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')
->orderByHeader($header);
$result = $pager->execute();
$rows=array();
foreach($result as $data){
if ($data->id != 0 && $data->id != 1) {
$operate = '<a href="/drupaladvance/mydata/form/mydata?num='.$data->id.'">Edit</a>|<a href="/drupaladvance/mydata/form/delete/'.$data->id.'">delete</a>';
//print the data from table
$rows[$data->id] = array(
'id' =>$data->id,
'name' => $data->name,
'mobile' => $data->mobilenumber,
'email' => $data->email,
'age' => $data->age,
'gender' => $data->gender,
'operation' => t($operate),
);
}
}
//display data in site
$form['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No users found'),
];
// Finally add the pager.
$form['pager'] = array(
'#type' => 'pager'
);
return $form;
}
}
?>
So that's all for display data by csutom module in drupal 8, now clear the cache and goto the modules page '/admin/modules' and find by name your module and enable it.
and go to the URL which we decide in '.routing.yml' file.
Note: If have any suggestions or issue regarding 'How to display records in custom module with pagination?' then you can ask by comments.
Step 1:
Create a folder with name of "gpcustom", this is our custom module name. Now create a file "gpcustom.info.yml" for create identity of module.
and the code for this file is..
name: Student Records
description: 'Display student records'
type: module
core: 8.x
Step 2:
Create a file with name of "gpcustom.routing.yml", here we are going to declare routing means path for display record page.
and the code for this file is..
google_feeds_list:
path: 'student/record'
defaults:
_controller: '\Drupal\gpcustom\Controller\gpcustomController::student_record'
_title: 'Student Records'
requirements:
_permission: 'access content'
// _controller: '\Drupal\gpcustom\Controller\gpcustomController::student_record' means '\Drupal\gpcustom\Controller\viewController' is file path for function and 'student_record' is the function name.
// _permission: 'access content' means anybody can access this page without login.
// path: 'student/record' means display record page url.
// _title: 'Student Records' means title of page.
Step 3:
Create a folder with this hierarchy '\src\Controller\' and file with name of "gpcustomController.php".
and the code for this file is..
<?php
namespace Drupal\gpcustom\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
/**
* Class DisplayTableController.
*
* @package Drupal\gpcustom\Controller
*/
class gpcustomController extends ControllerBase {
/**
* Returns a render-able array for a test page.
*/
public function student_record() {
//create table header
$header = array(
'id'=> t('SrNo'),
'name' => t('Candidate Name'),
'mobile' => t('Mobile Number'),
'email' => t('Email ID'),
'age' => t('Age'),
'gender' => t('Gender'),
'opt' => t('operations'),
);
//select records from table
$query = \Drupal::database()->select('mydata', 'm');
$query->fields('m', ['id','name','mobilenumber','email','age','gender']);
$pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(3);
// The actual action of sorting the rows is here.
$table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')
->orderByHeader($header);
$result = $pager->execute();
$rows=array();
foreach($result as $data){
if ($data->id != 0 && $data->id != 1) {
$operate = '<a href="/drupaladvance/mydata/form/mydata?num='.$data->id.'">Edit</a>|<a href="/drupaladvance/mydata/form/delete/'.$data->id.'">delete</a>';
//print the data from table
$rows[$data->id] = array(
'id' =>$data->id,
'name' => $data->name,
'mobile' => $data->mobilenumber,
'email' => $data->email,
'age' => $data->age,
'gender' => $data->gender,
'operation' => t($operate),
);
}
}
//display data in site
$form['table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No users found'),
];
// Finally add the pager.
$form['pager'] = array(
'#type' => 'pager'
);
return $form;
}
}
?>
So that's all for display data by csutom module in drupal 8, now clear the cache and goto the modules page '/admin/modules' and find by name your module and enable it.
and go to the URL which we decide in '.routing.yml' file.
Note: If have any suggestions or issue regarding 'How to display records in custom module with pagination?' then you can ask by comments.