Documentation

SortByFieldExtension extends Twig_Extension
in package

User: Victor Häggqvist Date: 3/4/15 Time: 2:07 AM

The base of the filter is borrowed from https://github.com/dannynimmo/craftcms-sortbyfield

I have extended it to also sort array structures

Table of Contents

getFilters()  : mixed
getFunctions()  : array<string|int, TwigFunction>
Returns a list of functions to add to the existing list.
getName()  : mixed
getNodeVisitors()  : array<string|int, NodeVisitorInterface>
Returns the node visitor instances to add to the existing list.
getOperators()  : array<string|int, array<string|int, mixed>>
Returns a list of operators to add to the existing list.
getTests()  : array<string|int, TwigTest>
Returns a list of tests to add to the existing list.
getTokenParsers()  : array<string|int, TokenParserInterface>
Returns the token parser instances to add to the existing list.
sortByFieldFilter()  : mixed
The "sortByField" filter sorts an array of entries (objects or arrays) by the specified field's value
isSortable()  : bool
Validate the passed $item to check if it can be sorted

Methods

getOperators()

Returns a list of operators to add to the existing list.

public getOperators() : array<string|int, array<string|int, mixed>>
Return values
array<string|int, array<string|int, mixed>>

First array of unary operators, second array of binary operators

getTests()

Returns a list of tests to add to the existing list.

public getTests() : array<string|int, TwigTest>
Return values
array<string|int, TwigTest>

sortByFieldFilter()

The "sortByField" filter sorts an array of entries (objects or arrays) by the specified field's value

public sortByFieldFilter(mixed $content[, mixed $sort_by = null ][, mixed $direction = 'asc' ]) : mixed

Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %}

Parameters
$content : mixed
$sort_by : mixed = null
$direction : mixed = 'asc'
Return values
mixed

isSortable()

Validate the passed $item to check if it can be sorted

private static isSortable( $item,  $field) : bool
Parameters
$item :

mixed Collection item to be sorted

$field :

string

Return values
bool

If collection item can be sorted

Search results