Pagination
26. April 2022
26. April 2022
In TYPO3 11 all widget functionality were removed:
https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.0/Breaking-92529-AllFluidWidgetFunctionalityRemoved.html
We have recreated this functionality with the new Pagination classes:
https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/Pagination/Index.html
<?php
declare(strict_types=1);
namespace VENDOR\YourExtension\Controller;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Pagination\SimplePagination;
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use VENDOR\YourExtension\Domain\Repository\YourRepository;
class YourController extends BaseController
{
/**
* @var YourRepository|null
*/
protected ?YourRepository $yourRepository = null;
/**
* @param YourRepository $yourRepository
* @return void
*/
public function injectYourRepository(YourRepository $yourRepository): void
{
$this->yourRepository = $yourRepository;
}
/**
* @return ResponseInterface
* @throws NoSuchArgumentException
*/
public function indexAction(): ResponseInterface
{
$itemsPerPage = 20;
$currentPageNumber = 1;
if ($this->request->hasArgument('page')) {
$currentPageNumber = (int)$this->request->getArgument('page');
}
$all = $this->yourRepository->findAll();
$paginator = new QueryResultPaginator($all, $currentPageNumber, $itemsPerPage);
$pagination = new SimplePagination($paginator);
$this->view->assignMultiple([
'results' => count($all),
'pagination' => $pagination,
'records' => $this->yourRepository->findPaginated($itemsPerPage, $currentPageNumber)
]);
return $this->htmlResponse();
}
}
<?php
declare(strict_types=1);
namespace VENDOR\YourExtension\Domain\Repository;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
class YourRepository extends Repository
{
/**
* @param int $limit
* @param int $offset
* @return QueryResultInterface
*/
public function findPaginated(int $limit=10, int $offset=0): QueryResultInterface
{
$query = $this->createQuery();
$query->setLimit($limit);
$query->setOffset($offset);
return $query->execute();
}
}
<html xmlns:f="http://typo3.org/ns/TYPO3/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<f:layout name="Default" />
<f:section name="Main">
<f:if condition="{records}">
<f:render section="Pagination" arguments="{_all}" />
<f:for each="{records}" as="record">
<p>{record.title}</p>
</f:for>
<f:render section="Pagination" arguments="{_all}" />
</f:if>
</f:section>
<f:section name="Pagination">
<f:if condition="{pagination}">
<p>{pagination.startRecordNumber}-{pagination.endRecordNumber} [{results}]</p>
<ul>
<f:if condition="{pagination.previousPageNumber}">
<li>
<f:link.action arguments="{page: pagination.previousPageNumber}">« zurück</f:link.action>
</li>
</f:if>
<f:if condition="{pagination.nextPageNumber}">
<li>
<f:link.action arguments="{page: pagination.nextPageNumber}">» weiter</f:link.action>
</li>
</f:if>
</ul>
</f:if>
</f:section>
</html>