diff --git a/src/Cms/Collection.php b/src/Cms/Collection.php index 938ad3609a..b6a80514cc 100644 --- a/src/Cms/Collection.php +++ b/src/Cms/Collection.php @@ -150,11 +150,11 @@ public function findBy(string $attribute, $value) * with an item for each group and a collection for each group. * * @param string|Closure $field - * @param bool $i Ignore upper/lowercase for group names + * @param bool $caseInsensitive Ignore upper/lowercase for group names * @return \Kirby\Cms\Collection * @throws \Kirby\Exception\Exception */ - public function group($field, bool $i = true) + public function group($field, bool $caseInsensitive = true) { if (is_string($field) === true) { $groups = new Collection([], $this->parent()); @@ -167,8 +167,12 @@ public function group($field, bool $i = true) throw new InvalidArgumentException('Invalid grouping value for key: ' . $key); } + $value = (string)$value; + // ignore upper/lowercase for group names - $value = $i === true ? Str::lower($value) : (string)$value; + if ($caseInsensitive === true) { + $value = Str::lower($value); + } if (isset($groups->data[$value]) === false) { // create a new entry for the group if it does not exist yet @@ -182,7 +186,7 @@ public function group($field, bool $i = true) return $groups; } - return parent::group($field, $i); + return parent::group($field, $caseInsensitive); } /** diff --git a/src/Toolkit/Collection.php b/src/Toolkit/Collection.php index 7f67040de2..22c2de6f49 100644 --- a/src/Toolkit/Collection.php +++ b/src/Toolkit/Collection.php @@ -517,21 +517,24 @@ protected function getAttributeFromObject($object, string $attribute) * Groups the elements by a given field or callback function * * @param string|Closure $field - * @param bool $i * @return \Kirby\Toolkit\Collection A new collection with an element for * each group and a subcollection in * each group * @throws \Exception if $field is not a string nor a callback function */ - public function group($field, bool $i = true) + public function group($field, bool $caseInsensitive = true) { // group by field name if (is_string($field) === true) { - return $this->group(function ($item) use ($field, $i) { + return $this->group(function ($item) use ($field, $caseInsensitive) { $value = $this->getAttribute($item, $field); // ignore upper/lowercase for group names - return $i === true ? Str::lower($value) : (string)$value; + if ($caseInsensitive === true) { + $value = Str::lower($value); + } + + return (string)$value; }); }