PHP code example of burnbright / silverstripe-listsorter

1. Go to this page and download the library: Download burnbright/silverstripe-listsorter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

burnbright / silverstripe-listsorter example snippets


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

public function getSortableChildren() {
	$list = $this->Children();
	$list = $this->getSorter()->sortList($list);
	return $list;
}




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
        ]);
    }
}