forked from TYPO3-Documentation/TYPO3CMS-Reference-CoreApi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Extract code snippets into dedicated files (TYPO3-Documentatio…
…n#4803) Additionally, embed code examples into an Extbase controller class. Releases: main, 12.4
- Loading branch information
1 parent
f042fa6
commit 69abe01
Showing
4 changed files
with
124 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Documentation/ApiOverview/Pagination/_ArrayPaginatorExampleController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Controller; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use TYPO3\CMS\Core\Pagination\ArrayPaginator; | ||
use TYPO3\CMS\Core\Pagination\SimplePagination; | ||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
|
||
final class ExampleController extends ActionController | ||
{ | ||
public function myAction(): ResponseInterface | ||
{ | ||
// For better demonstration we create fixed items, in real | ||
// world usage a list of models is used instead. | ||
$itemsToBePaginated = ['apple', 'banana', 'strawberry', 'raspberry', 'pineapple']; | ||
$itemsPerPage = 2; | ||
$currentPageNumber = 3; | ||
|
||
$paginator = new ArrayPaginator($itemsToBePaginated, $currentPageNumber, $itemsPerPage); | ||
$paginator->getNumberOfPages(); // returns 3 | ||
$paginator->getCurrentPageNumber(); // returns 3, basically just returns the input value | ||
$paginator->getKeyOfFirstPaginatedItem(); // returns 4 | ||
$paginator->getKeyOfLastPaginatedItem(); // returns 4 | ||
|
||
$pagination = new SimplePagination($paginator); | ||
$pagination->getAllPageNumbers(); // returns [1, 2, 3] | ||
$pagination->getPreviousPageNumber(); // returns 2 | ||
$pagination->getNextPageNumber(); // returns null | ||
|
||
// ... more logic ... | ||
|
||
$this->view->assignMultiple( | ||
[ | ||
'pagination' => $pagination, | ||
'paginator' => $paginator, | ||
], | ||
); | ||
|
||
return $this->htmlResponse(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Documentation/ApiOverview/Pagination/_ArrayPaginatorExamplePagination.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<ul class="pagination"> | ||
<f:for each="{pagination.allPageNumbers}" as="page"> | ||
<li class="page-item"> | ||
<f:link.action | ||
arguments="{currentPageNumber:page}" | ||
class="page-link {f:if(condition:'{currentPageNumber}=={page}',then:'active')}" | ||
> | ||
{page} | ||
</f:link.action> | ||
</li> | ||
</f:for> | ||
</ul> | ||
|
||
<f:for each="{paginator.paginatedItems}" as="item"> | ||
{item} | ||
</f:for> |
51 changes: 51 additions & 0 deletions
51
Documentation/ApiOverview/Pagination/_SlidingWindowExampleController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyVendor\MyExtension\Controller; | ||
|
||
use MyVendor\MyExtension\Domain\Repository\ExampleRepository; | ||
use Psr\Http\Message\ResponseInterface; | ||
use TYPO3\CMS\Core\Pagination\SlidingWindowPagination; | ||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator; | ||
|
||
final class ExampleController extends ActionController | ||
{ | ||
public function __construct( | ||
private readonly ExampleRepository $exampleRepository, | ||
) {} | ||
|
||
public function myAction(): ResponseInterface | ||
{ | ||
$allItems = $this->exampleRepository->findAll(); | ||
|
||
$currentPage = $this->request->hasArgument('currentPageNumber') | ||
? (int)$this->request->getArgument('currentPageNumber') | ||
: 1; | ||
$itemsPerPage = 10; | ||
$maximumLinks = 15; | ||
|
||
$paginator = new QueryResultPaginator( | ||
$allItems, | ||
$currentPage, | ||
$itemsPerPage, | ||
); | ||
$pagination = new SlidingWindowPagination( | ||
$paginator, | ||
$maximumLinks, | ||
); | ||
|
||
// ... more logic ... | ||
|
||
$this->view->assign( | ||
'pagination', | ||
[ | ||
'pagination' => $pagination, | ||
'paginator' => $paginator, | ||
], | ||
); | ||
|
||
return $this->htmlResponse(); | ||
} | ||
} |