Search code examples
typo3extbasetypo3-10.xtypo3-11.x

T3PO3 v10: How to use RouteEnhancer with ArrayPaginator?


I'm using the ArrayPaginator for my Extbase list view, since Fluid widget functionality will no longer be available in TYPO3 v11.

And I'm currently trying to use the RouteEnhancer with this.

My list action just receives the argument for the current page as integer.

And the code for the RouteEnhancer looks like this:

  RefList:
    type: Extbase
    limitToPages:
      - 142
    extension: Ref
    plugin: Pi2
    routes:
      -
        routePath: /seite-{page}
        _controller: 'Ref::list'
        _arguments:
          page: currentPage
    defaultController: 'Ref::list'
    requirements:
      page: \d+
    aspects:
      page:
        type: StaticRangeMapper
        start: '1'
        end: '1000'

I've seen a lot examples for the Fluid pagination widget that contained the value @widget_0/currentPage for the argument page.

The URL is generated correctly if I switch to another page but the content displayed from the list view is still the same as on page 1.

What am I missing here?


Solution

  • You can us a QueryResultPaginator instead of a ArrayPaginator.

    Route

    routeEnhancers:
      RefList:
        type: Extbase
        extension: Ref
        plugin: Pi2
        routes:
          - routePath: '/'
            _controller: 'Ref::list'
          - routePath: '/{page}'
            _controller: 'Ref::list'
            _arguments:
              page: current-page
        defaultController: 'Ref::list'
        aspects:
          current-page:
            type: StaticRangeMapper
            start: '1'
            end: '100'
    

    RefController.php Get current-page from request

    public function listAction()
    {
        $refs = $this->refRepository->findAll();
    
        $currentPage = $this->request->hasArgument('current-page') ? $this->request->getArgument('current-page') : 1;
        $paginatedRefs = new QueryResultPaginator($refs, $currentPage, $this->itemsPerPage);
        $simplePagination = new SimplePagination($paginatedRefs);
        $pagination = $this->buildSimplePagination($simplePagination, $paginatedRefs);
    
        $this->view->assignMultiple([
            'refs' => $paginatedRefs,
            'pagination' => $pagination,
        ]);
    }
    
    
    /**
     * Build simple pagination
     *
     * @param SimplePagination $simplePagination
     * @param QueryResultPaginator $paginator
     * @return array
     */
    protected function buildSimplePagination(SimplePagination $simplePagination, QueryResultPaginator $paginator): array
    {
        $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)
        ];
    }
    

    List.html add pagination template

    <f:for each="{refs.paginatedItems}" as="ref">
        <f:render partial="List/ListItem" arguments="{ref: ref}" />
    </f:for>
    <f:render partial="List/Pagination" arguments="{pagination:pagination, action:'list'}" />
    

    Partial List/Pagination.html

    add links for pagination: previous

    <f:link.action action="{action}" arguments="{current-page:pagination.previousPageNumber}">previous</f:link.action>
    

    add links for pagination: pages

    <f:link.action action="{action}" arguments="{current-page:page}">{page}</f:link.action>
    

    add links for pagination: next

    <f:link.action action="{action}" arguments="{current-page: pagination.nextPageNumber}">next</f:link.action>