From 9590f61229a6e23c4f4ed91d34e3224804f69d40 Mon Sep 17 00:00:00 2001 From: Victor Häggqvist Date: Wed, 27 Jan 2016 00:58:22 +0100 Subject: add doctrine collection support --- src/SortByFieldExtension.php | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/SortByFieldExtension.php b/src/SortByFieldExtension.php index 5241b9e..0eda94a 100644 --- a/src/SortByFieldExtension.php +++ b/src/SortByFieldExtension.php @@ -31,8 +31,15 @@ class SortByFieldExtension extends \Twig_Extension { * Usage: {% for entry in master.entries|sortbyfield('ordering', 'desc') %} */ public function sortByFieldFilter($content, $sort_by = null, $direction = 'asc') { + + if (is_a($content, 'Doctrine\Common\Collections\Collection')) { + $content = $content->toArray(); + } + if (!is_array($content)) { throw new \InvalidArgumentException('Variable passed to the sortByField filter is not an array'); + } elseif (count($content) < 1) { + return $content; } elseif ($sort_by === null) { throw new Exception('No sort by parameter passed to the sortByField filter'); } elseif (!self::isSortable(current($content), $sort_by)) { -- cgit v1.2.3