Skip to content

Latest commit

 

History

History
172 lines (132 loc) · 6.55 KB

File metadata and controls

172 lines (132 loc) · 6.55 KB

Pagination

Requirements

  • Include TypoScript at EXT:handlebars_components/Configuration/TypoScript/Pagination

Description

This component enables the rendering of a pagination using Handlebars. A NumberedPagination is provided for this purpose. It renders a pagination in the following form:

[1] … [3] [4] [5] … [10]

The rendering is made up of several individual components:

Pagination

The NumberedPagination corresponds to a representation of the generated pagination. It is based on a concrete implementation of the PaginatorInterface from TYPO3 core. It holds the information about items to be paginated as well as parameters for building the pagination, such as the number of elements per page or the current page number.

Pagination factory

For simplified instantiation of a pagination, the extension provides a PaginationFactory. This creates an instance of NumberedPagination including the underlying paginator on the basis of the items to be paginated and other parameters. Currently, the following paginators are supported:

Item type Resulting paginator Description
QueryResultInterface QueryResultPaginator Extbase query result
SearchResultSet ResultsPaginator Solr search result
iterable ArrayPaginator Default array paginator

💡 Tip: By extending the PaginationFactory::buildPaginator() method, you can add support for other paginators, if needed.

Template variables

The extension already provides a simple variant for rendering pagination templates. For this purpose, a PaginationVariablesResolver exists that resolves a transmitted NumberedPagination object into associated template variables. The resolver uses the following template structure by default:

items
└── .[]
    ├── label
    ├── link
    └── current

Explanation: The items array contains multiple objects for mapping individual elements in the pagination. Each item represents either a page or a placeholder (). Placeholders are only given a label. Pages, on the other hand, additionally receive the generated link and the indication whether it is the current page number.

Pagination linkers

Since the linking of individual pages can look different depending on the context and the paginator used, this is done using so-called pagination linkers. The PaginationLinkerInterface is available for this purpose. The extension already provides the following pagination linkers:

Type Description Class name
Default linker Appends a query parameter page to the current request URL. DefaultPaginationLinker
Solr linker Uses the SearchUriBuilder from EXT:solr to generate URLs. SolrSearchResultPaginationLinker

Example

Instantiate the pagination in your data provider:

# Classes/Data/MyBeautifulProvider.php

namespace Vendor\Extension\Data;

use Fr\Typo3Handlebars\Data\DataProviderInterface;
use Fr\Typo3Handlebars\Data\Response\ProviderResponseInterface;
use Cpsit\Typo3HandlebarsComponents\Pagination\PaginationFactory;
use Vendor\Extension\Data\Response\MyBeautifulProviderResponse;

final class MyBeautifulProvider implements DataProviderInterface
{
    private PaginationFactory $paginationFactory;

    public function __construct(PaginationFactory $paginationFactory)
    {
        $this->paginationFactory = $paginationFactory;
    }

    public function get(array $data): ProviderResponseInterface
    {
        $items = $this->fetchItems();
        $pagination = $this->paginationFactory->get($items);

        // ...

        return new MyBeautifulProviderResponse($pagination, /* ... */);
    }
}

In your presenter, create the template variables and render your final template:

# Classes/Presenter/MyBeautifulPresenter.php

namespace Vendor\Extension\Presenter;

use Fr\Typo3Handlebars\Data\Response\ProviderResponseInterface;
use Fr\Typo3Handlebars\Exception\UnableToPresentException;
use Fr\Typo3Handlebars\Presenter\AbstractPresenter;
use Cpsit\Typo3HandlebarsComponents\Pagination\Linker\DefaultPaginationLinker;
use Cpsit\Typo3HandlebarsComponents\Presenter\VariablesResolver\PaginationVariablesResolver;
use Vendor\Extension\Data\Response\MyBeautifulProviderResponse;

final class MyBeautifulPresenter extends AbstractPresenter
{
    private DefaultPaginationLinker $paginationLinker;
    private PaginationVariablesResolver $paginationVariablesResolver;

    public function __construct(
        DefaultPaginationLinker $paginationLinker,
        PaginationVariablesResolver $paginationVariablesResolver
    ) {
        $this->paginationLinker = $paginationLinker;
        $this->paginationVariablesResolver = $paginationVariablesResolver;
    }

    public function present(ProviderResponseInterface $data): string
    {
        if (!($data instanceof MyBeautifulProviderResponse)) {
            throw new UnableToPresentException('Received unexpected response from provider.', 1647948612);
        }

        // Add default variables
        $renderData = [
            // ...
        ];

        // Resolve pagination variables
        $renderData['pagination'] = $this->paginationVariablesResolver->resolve(
            $data->getPagination(),
            $this->paginationLinker
        );

        return $this->renderer->render(/* ... */, $renderData);
    }
}