The Kirby Alphabetise Plugin processes a Kirby CMS $page or $tag array and returns a new array that is organized alphabetically or numerically with labeled subsections.
This project is based on a fork of shoesforindustry/kirby-plugins-alphabetise by Russ Baldwin.
Compatible with both Kirby 3 and Kirby 4.
- Clone this repository.
- Move the folder to
site/plugins - Rename the folder to
alphabetise
- Download this repository.
- Decompress the
master.ziparchive. - Move the folder to
site/plugins - Rename the folder to
alphabetise
The first argument you pass is the sorted $page or $tag array you want to alphabetise.
The key argument of the second array determines what to alphabetise by.
This argument should be a string like title or date. Note 1
The values passed to sortBy and key are usually the same.
For example, you would include this in your template:
<?php $alphabetise = alphabetise($page->children()->listed()->sortby('title'), array('key' => 'title')); ?><?php foreach($alphabetise as $letter => $items) : ?>
<h2><?= str::upper($letter) ?></h2>
<ul>
<?php foreach($items as $item): ?>
<li>
<a href="<?= $item->url()?>">
<?= $item->title()?>
</a>
</li>
<?php endforeach ?>
</ul>
<?php endforeach ?>Result:
- 1
- 1a
- 1b
- 2
- 2a
- 2b
- A
- Aa
- Ab
- B
- Ba
- Bb
The sort order is specified by using the orderby flag when calling the alphabetise function. Note 2
By default, numbers and symbols are listed before letters. Note 3
This is the same result as setting the orderby flag to SORT_REGULAR or SORT_STRING Note 4
- 1
- 2
- @
- A
- B
For example:
<?php $alphabetise = alphabetise($page->children()->listed()->sortby('title'), array('key' => 'title', 'orderby'=>SORT_REGULAR));?>To list letters before numbers, set the orderby flag to SORT_NUMERIC Note 5
- A
- B
- @
- 1
- 2
For example:
<?php $alphabetise = alphabetise($page->children()->listed()->sortby('title'), array('key' => 'title', 'orderby'=>SORT_NUMERIC));?>1 The explode function used for array parsing uses the tilde character ~ as the separator value. If this character appears in one of the key values, especially at the beginning of a string, you could run into sorting problems. You can manually change the separator value if required.
2 The value of the orderby flag is not a string.
3 PHP 8.2 changed the way the ksort function behaves when using the SORT_REGULAR flag. In previous versions of PHP, the SORT_REGULAR flag listed numeric characters after alphabetical characters. It now arranges numeric characters before alphabetical characters, the same as the SORT_STRING and SORT_NATURAL flags.
4 This plugin uses the PHP ksort function, so other sorting type parameters might work, but have not been tested.
5 Using the SORT_NUMERIC flag may result in unexpected results if any of your key values consist of single characters.
- Added check for duplicate
keyvalues. - Modified handling of single character
keyfield text. - Changed plugin namespace from
shoesforindustrytosplorpto match fork. - Updated documentation.
- Added field to
composer.jsonfor link in the Kirby Panel.
- Additional fixes for Kirby 3 compatibility.
- Renamed
alphabetise.phptoindex.phpfor Kirby 3 compatibility. - Renamed
package.jsontocomposer.json - Updated documentation.
- Added
orderbykey for alternative sort order.
- Fixed
Array to string conversionerror.
- Fixed a small bug introduced in the 0.0.6 update.
- Fixed bug when using only a single character of text for a
keyfield. - Updated documentation to remove workaround.
- Discovered bug when using only a single character of text for a
keyfield. - Updated documentation with explanation and possible workaround.
- Bug fix for spaces in
explodekey, now'~'instead of space' ' - Updated page code with a pre-sort
'sortby('title')' - Updated documentation and examples.
- Updated documentation.
- Added error handling code.
- Updated documentation.
- Initial release.
Russ Baldwin
shoesforindustry.net
Grant Hutchinson
splorp.com