diff --git a/LICENSE.md b/LICENSE.md index a176cf0..5b82e1e 100755 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 hana+nils · Büro für Gestaltung +Copyright (c) 2020-2023 hana+nils · Büro für Gestaltung Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 53d0801..763d200 100755 --- a/README.md +++ b/README.md @@ -99,7 +99,26 @@ $pages->toNumericList('date', true, 'my-year-overview/{{page.date.toDate('Y')}}' You can use Kirby's template syntax with [query language](https://getkirby.com/docs/guide/blueprints/query-language) to fetch any information from the current context, e. g. the current `$user`, `$page` or `$file` object. The `$kirby` and `$site` objects are also available. -# Collection method +# Content methods + +When dealing with a single page or user, there are methods to generate lists from content field: + +```php +// Given the fields name and job, creates "Jane Doe, astrophysicist" +echo $user->asList(['name', 'job']); + +// Given the fields start and end, creates "2020–2023" +echo $page->asNumericList(['start', 'end']); +``` + +Both methods, `asList` and `asNumericList`, support setting a custom conjunction via a secondary attribute: + +```php +// Given the fields name and job, creates "Jane Doe: astrophysicist" +echo $page->asList(['name', 'job'], ': '); +``` + +# Collection methods The plugin also features a general, more simple collection method which is a shortcut the `naturalList()` helper and only allows for a custom conjunction: @@ -148,5 +167,4 @@ numericList($data, true); # License -This plugin is provided freely under the [MIT license](LICENSE.md) by [hana+nils · Büro für Gestaltung](https://hananils.de). -We create visual designs for digital and analog media. +This plugin is provided freely under the [MIT license](LICENSE.md) by [hana+nils · Büro für Gestaltung](https://hananils.de). We create visual designs for digital and analog media. diff --git a/composer.json b/composer.json index b1a4244..c034708 100755 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Methods to convert Kirby objects to natural comma-speparated lists", "type": "kirby-plugin", "license": "MIT", - "version": "2.1.0", + "version": "2.2.0", "authors": [ { "name": "hana+nils · Büro für Gestaltung", diff --git a/index.php b/index.php index 8118e11..5787bb6 100755 --- a/index.php +++ b/index.php @@ -118,6 +118,30 @@ return naturalList($data, $conjunction); } ], + 'userMethods' => [ + 'asList' => function ($fields = [], $conjunction = false) { + $data = []; + + foreach ($fields as $field) { + $data[] = $this->content() + ->get($field) + ->value(); + } + + return naturalList($data, $conjunction); + }, + 'asNumericList' => function ($fields = [], $conjunction = false) { + $data = []; + + foreach ($fields as $field) { + $data[] = $this->content() + ->get($field) + ->value(); + } + + return numericList($data, $conjunction); + } + ], 'pagesMethods' => [ 'toList' => function ( $field = 'title', @@ -150,6 +174,30 @@ return naturalList($data, $conjunction); } ], + 'pageMethods' => [ + 'asList' => function ($fields = [], $conjunction = false) { + $data = []; + + foreach ($fields as $field) { + $data[] = $this->content() + ->get($field) + ->value(); + } + + return naturalList($data, $conjunction); + }, + 'asNumericList' => function ($fields = [], $conjunction = false) { + $data = []; + + foreach ($fields as $field) { + $data[] = $this->content() + ->get($field) + ->value(); + } + + return numericList($data, $conjunction); + } + ], 'filesMethods' => [ 'toList' => function ( $field = 'filename',