Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Example Pagination for TYPO3v10

An efficient way to use Extbase Pagination in your custom extension, inspired by blog entry from undkonsorten (in german):

https://blog.undkonsorten.com/native-pagination-in-typo3-10

Add PHP class PaginationService to your extension:

Fx \Vendor\MyExtension\Service\PaginationService

Add method to assign pagination to FLUID view:

/**
 * Assign pagination to current view object
 *
 * array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface $items
 */
protected function assignPagination($items)
{
    if ((int)$this->settings['pagination']['disable'] != 1) {
        $paginationService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(PaginationService::class);
        $currentPage = $this->request->hasArgument('currentPage') ? $this->request->getArgument('currentPage') : 1;
        $paginator = $paginationService->getPaginator($items, $currentPage);
        $pagination = $paginationService->getPagination($paginator);
    }
    $this->view->assignMultiple(
        [
            'pagination' => $pagination,
            'paginator' => $paginator
        ]
    );
}

Use method in your controller list action for your own entries:

/**
 * View list
 */
public function listAction()
{
    $myEntries = $this->addressRepository->findAll();
    $this->view->assign('myEntries', $myEntries);
    $this->assignPagination($myEntries);
}

Then you can access all pagination configuration in your FLUID template as shown in

EXT:operations https://github.com/kanow/operations

Thx to Karsten Nowak for sharing.

<?php
namespace Vendor\MyExtension\Service;
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
/**
* Class PaginationService
*
* @package TYPO3
* @subpackage tx_myextension
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 2 or later
*/
class PaginationService
{
/**
* Get paginator query result
*
* @param array|QueryResultInterface $items
* @param int $currentPage
* @param int $itemsPerPage
* @return QueryResultPaginator|null $paginator
*/
public function getPaginator($items, $currentPage = 1, $itemsPerPage = 10)
{
return new QueryResultPaginator($items, (int)$currentPage, (int)$itemsPerPage);
}
/**
* Get pagination configuration
*
* @param QueryResultPaginator $paginator
* @return array $pagination
*/
public function getPagination(QueryResultPaginator $paginator)
{
$simplePagination = new SimplePagination($paginator);
$firstPage = $simplePagination->getFirstPageNumber();
$lastPage = $simplePagination->getLastPageNumber();
return [
'lastPageNumber' => $lastPage,
'firstPageNumber' => $firstPage,
'nextPageNumber' => $simplePagination->getNextPageNumber(),
'previousPageNumber' => $simplePagination->getPreviousPageNumber(),
'startRecordNumber' => $simplePagination->getStartRecordNumber(),
'endRecordNumber' => $simplePagination->getEndRecordNumber(),
'currentPageNumber' => $paginator->getCurrentPageNumber(),
'pages' => range($firstPage, $lastPage)
];
}
}
@jokumer

This comment has been minimized.

Copy link
Owner Author

@jokumer jokumer commented Apr 21, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment