pager_default_initialize() is a function in Drupal that is used to initialize the pagination system. It helps to break down the output of large sets of data into multiple pages and manage those pages.
The function takes two parameters:
1. $total — the total number of items that need to be paginated.
2. $limit — the number of items to display on a single page.
Drupal uses this method in contexts where long lists of data (such as search results, content lists, etc.) need to be presented to users in manageable portions.
Example usage:
php
$limit = 10; // Number of items to display per page
$total = db_query('SELECT COUNT(*) FROM {example_table}')->fetchField(); // Counting the total number of items
// Initializing the pagination
pager_default_initialize($total, $limit);
// Retrieving the current page
$current_page = pager_find_page();After this, you can use the theme('pager') function to render the pagination controls in the template.
This allows for a more user-friendly interface by preventing data overload on a single page.
The pager_default_initialize() function was present in Drupal 7. However, in later versions of Drupal (starting from Drupal 8), the pagination system was significantly reworked, and functions like pager_default_initialize() are no longer used.
In Drupal 8 and newer versions, pagination is implemented through the rendering system, controllers, and objects like PagerPaginationInterface, along with other services related to Symfony.
So, if you're working with Drupal 7, pager_default_initialize() is relevant. But in Drupal 8 and above, a different approach is used to implement pagination.
In Drupal 8 and above, pagination is no longer implemented using functions like pager_default_initialize(). Instead, it relies on objects and rendering, following the modern architectural approach with Symfony components.
Here’s an overview of the pagination system and an example of how it is implemented in Drupal 8, 9, and 11.
Key Components:
1. Pagerfanta – This is a library that Drupal uses to manage pagination.
2. PagerPaginationInterface – An interface used to create and display pagination.
3. Custom Query – Used to fetch data either through Doctrine or regular SQL.
Example: Creating Custom Pagination in a Controller
php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Symfony\Component\HttpFoundation\Request;
class MyController extends ControllerBase {
public function myPage(Request $request) {
// Get the current page from the URL (starting from 0)
$current_page = $request->query->get('page', 0);
$limit = 10; // Number of items per page
// Connect to the database and perform the query
$query = Database::getConnection()->select('my_table', 't')
->fields('t', ['id', 'name', 'description']);
$query->range($current_page * $limit, $limit); // Set the range for the query
// Execute the query
$results = $query->execute()->fetchAll();
// Count the total number of records (for pagination)
$total = Database::getConnection()->select('my_table', 't')
->countQuery()
->execute()
->fetchField();
// Use the pager service to create pagination
$pager = \Drupal::service('pager.manager')->createPager($total, $limit);
$pager->setCurrentPage($current_page);
return [
'#theme' => 'item_list', // Display the result as a list
'#items' => $results, // Data to display
'#pager' => ['#type' => 'pager'], // Render the pagination
];
}
}Steps:
1. Get the current page: You can retrieve the current page using the request parameter $request->query->get('page').
2. Limit the query: Use the range() method to define which records to display on each page.
3. Count total items: This step is necessary to properly calculate pagination.
4. Create pagination: The pager.manager service creates a pager object, which handles page navigation.
5. Render pagination: In the template, pagination is rendered using #type => 'pager'.
Example for displaying pagination in Twig:
twig
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
{{ pager }}Important Points:
- Pagination is managed through the pager.manager service, which handles most of the page management tasks.
- Pagination rendering is done through Twig using the {{ pager }} tag.
Thus, pagination in Drupal 8 and above is more structured and relies on modern practices using services and objects rather than functional calls as in Drupal 7.
In Drupal 11, the pagination system retains the approach introduced in Drupal 8 and 9, using Symfony components and object-oriented programming.
Key components of pagination in Drupal 11:
1. PagerManager — This service manages pagination.
2. PagerInterface — An interface representing a paginator, helping manage pages.
3. Doctrine ORM or Custom Queries — Used for executing queries with pagination support.
4. Twig Templates — For rendering pagination in templates.
Example: Creating pagination in Drupal 11
1. Creating a Controller:
php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Database\Database;
class MyController extends ControllerBase {
public function myPaginatedPage(Request $request) {
// Current page, starts from 0
$current_page = $request->query->get('page', 0);
$limit = 10; // Number of items per page
// Querying data from the database
$query = Database::getConnection()->select('my_table', 't')
->fields('t', ['id', 'name', 'description']);
$query->range($current_page * $limit, $limit); // Setting range for pagination
// Fetching results
$results = $query->execute()->fetchAll();
// Counting the total number of records for pagination
$total_items = Database::getConnection()->select('my_table', 't')
->countQuery()
->execute()
->fetchField();
// Creating paginator using pager manager
$pager = \Drupal::service('pager.manager')->createPager($total_items, $limit);
$pager->setCurrentPage($current_page);
return [
'#theme' => 'item_list', // Using item_list theme for display
'#items' => $results, // Array of data to display
'#pager' => ['#type' => 'pager'], // Adding pagination to the render
];
}
}2. Displaying in a Twig template:
twig
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
{{ pager }}Key steps:
1. Initializing the current page: Drupal 11 continues to use the page query parameter, which specifies the current page.
2. Creating a query with a limit on the number of items: The range() method is used to specify the data range for pagination.
3. Counting total items: This step is necessary to ensure proper pagination navigation.
4. pager.manager service: This service handles the creation of the paginator and can support different pagination styles.
5. Rendering pagination: The rendering system uses Twig to display pagination on the page.
Important points:
- Pagination in Drupal 11 remains similar to the implementation in Drupal 8 and 9.
- The process is flexible due to the use of Symfony components and services.
- Rendering is done through the theming system and Twig templates.
Drupal 11 continues to follow modern architectural standards, providing convenient tools for handling pagination within standard controllers and services.
In Drupal 8 and later, including Drupal 11, pagination can be implemented using the PagerSelectExtender class, which extends the standard SQL query and adds pagination support directly at the database query level.
Example of using PagerSelectExtender for pagination:
1. Creating a Controller:
php
namespace Drupal\my_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Database;
use Symfony\Component\HttpFoundation\Request;
class MyController extends ControllerBase {
public function paginatedResults(Request $request) {
// Number of items per page
$limit = 10;
// Create a query using pager select extender
$query = Database::getConnection()->select('my_table', 't')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
// Select the required fields
$query->fields('t', ['id', 'name', 'description']);
// Set the limit for items per page
$query->limit($limit);
// Execute the query and fetch the results
$results = $query->execute()->fetchAll();
return [
'#theme' => 'item_list',
'#items' => $results,
// Pagination through 'pager'
'#pager' => [
'#type' => 'pager',
'#quantity' => 5, // Number of pages displayed in the pagination control
],
];
}
}Explanation:
1. Query Extension: The method extend('Drupal\Core\Database\Query\PagerSelectExtender') extends the base SQL query, adding pagination support. The query will now return data in chunks, based on the pagination setup.
2. Setting the limit: Using the limit() method specifies how many records should be returned per page.
3. Rendering Pagination: In the return array, the #pager key is added, which is responsible for rendering the navigation between pages.
4. #quantity parameter: This defines the number of visible pages in the pagination control (for example, if you have many pages, it will only show a limited number).
Example of rendering in Twig:
twig
<ul>
{% for item in items %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
{{ pager }}Important points:
- PagerSelectExtender automatically handles pagination, making it easier to create complex queries with built-in pagination.
- The results and pagination rendering are handled by Drupal's standard rendering system and Twig templates.
- This approach is useful when dealing with large datasets and needing an easy way to implement pagination without too much overhead.
Thus, PagerSelectExtender is a powerful tool for managing pagination when constructing SQL queries in Drupal 8, 9, 10, and 11.
Comments