Skip to content

Commit

Permalink
Add content methods to generate lists from multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshoerrmann committed Feb 16, 2023
1 parent 7450dc4 commit d88d793
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit d88d793

Please sign in to comment.