Reputation: 342
I would like to create links that let the user sort the product list in cart_products ascending and descending.
For this I created a Fluid-link in Grid.html
of cart_products that passes an argument for sorting to the controller:
<f:link.action action="list" arguments="{sorting:'up'}">Sorting up</f:link.action>
The method listAction()
in ProductController.php
gets the argument with:
if ($this->request->hasArgument('sorting')) {
$sorting = $this->request->getArgument('sorting');
}
With this if-statement I control what is happening based on the given argument:
if ($sorting === "up") {
// Get all products sorted ascending
} elseif ($sorting === "down"){
// Get all products sorted decending
}
The products are received with the following command (original):
$products = $this->productRepository->findDemanded($demand);
The documentation says that the following function does the sorting:
$query->setOrderings(
[
'organization.name' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING,
'title' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
]
);
I would like to know how to combine the both parts to receive the products ordered as wished.
Upvotes: 1
Views: 292
Reputation: 2262
Change your view helper to directly use sorting argument
<f:link.action action="listSorted" arguments="{sorting:'asc'}">Sorting up</f:link.action>
Add your own controller action (extending class ProductController)
<?php
// ...
public function listSortedAction(int $currentPage = 1): void
{
$demand = $this->createDemandObjectFromSettings($this->settings);
// ...
// Code from cart_products/Classes/Controller/ProductController.php listAction()
// ...
// instead of $products = $this->productRepository->findDemanded($demand);
if ($this->request->hasArgument('sorting')) {
$sorting = $this->request->getArgument('sorting');
$products = $this->productRepository->findDemandedSorted($demand,$sorting);
}
//...
?>
Then add you own repository function (extending class ProductRepository)
<?php
public function findDemandedSorted(ProductDemand $demand, $sortOrder)
{
$query = $this->createQuery();
// ...
// Code from cart_products/Classes/Domain/Repository/Product/ProductRepository.php findDemanded(ProductDemand $demand)
// ...
// instead of
// if ($orderings = $this->createOrderingsFromDemand($demand)) {
// $query->setOrderings($orderings);
// }
$query->setOrderings($sortOrder);
return $query->execute();
}
Upvotes: 2