Skip to content

Commit

Permalink
Merge pull request #20 from silvershop/wernerkrauss-patch-1
Browse files Browse the repository at this point in the history
Update README.md
  • Loading branch information
wilr authored Oct 6, 2023
2 parents d5835bd + 18ea483 commit d9ee46a
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ A front-end control for sorting SilverStripe lists easily. The aim of this modul

## Requirements

* SilverStripe 4+
* SilverStripe 4+ or 5+

## Usage

There are a few ways you can define sort options within an array.

Make a public function on your controller:
```php
function getSorter(){
public function getSorter(){
$sorts = [
'Title', //DB field name only
'Popularity' => 'Popularity DESC', //map title to sort sql
'Price' => array('BasePrice' => 'ASC'), //map title to data list sort
ListSorter_Option::create('Age', 'Created DESC', //object
new ListSorter_Option('Age', array('Created' => 'ASC')) //reverse
'Price' => ['BasePrice' => 'ASC'], //map title to data list sort
ListSorter_Option::create('Age', ['Created' => 'DESC'], //object
ListSorter_Option::create('Age', ['Created' => 'ASC']) //reverse
)
;
return new ListSorter($this->request,$sorts);
return ListSorter::create($this->request,$sorts);
}
```

Expand All @@ -49,3 +49,57 @@ Use my template or roll your own.
<% end_loop %>
</ul>
```

## Usage with Silvershop

Silvershop's PageCategoryController comes with some sortings predefined. If you want to define your own sorting possibilities, you can add an Extension to ProductCategory like this:

```php
<?php

namespace MyNamespace\SilverShop\Extensions;

use SilverShop\ListSorter\ListSorter;
use SilverShop\ListSorter\ListSorterOption;
use SilverStripe\Core\Extension;
use SilverStripe\Security\Security;

class ProductCategorySorting extends Extension
{

public function updateSorter(ListSorter $sorter)
{

$basePriceOptionDESC = ListSorterOption::create('BasePrice highest first', ['BasePrice' => 'DESC']);
$basePriceOptionASC = ListSorterOption::create('BasePrice lowest first', ['BasePrice' => 'ASC']);

$titleOptionASC = ListSorterOption::create('Title a-z', ['Title' => 'ASC']);
$titleOptionDESC = ListSorterOption::create('Title z-a', ['Title' => 'DESC']);

$newestOption = ListSorterOption::create('Newest first', ['Created' => 'DESC']);

$popularityOption = ListSorterOption::create('Most Popular', ['Popularity' => 'DESC']);

//overwrite all settings
//you can use $sorter->addSortOption($option) if you want to add a sort option

$sorter->setSortOptions([
$basePriceOptionASC,
$basePriceOptionDESC,
$titleOptionASC,
$titleOptionDESC,
$newestOption,
$popularityOption
]);
}
}

```

Then add this extension to ProductCategoryController in your config:

```yaml
SilverShop\Page\ProductCategoryController:
extensions:
- MyNamespace\Silvershop\Extensions\ProductCategorySorting
```

0 comments on commit d9ee46a

Please sign in to comment.