The Alphabetise Kirby plugin will alphabetise a given Kirby page array or tag array and return it for further processing/display as an alphabetised array.
PHP
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
alphabetise.php
package.json
readme.md

readme.md

Alphabetise Plugin

What is it?

The Alphabetise plugin will alphabetise a given Kirby CMS page array or tag array and return it for further processing or display as an alphabetised array.

Installation

Put all the files into your site/plugins/alphabetise folder or use the Kirby CLI. To install using the Kirby CLI, from the command line in your project folder:

Install the plugin:

kirby plugin:install shoesforindustry/kirby-plugins-alphabetise

Update the plugin:

kirby plugin:update shoesforindustry/kirby-plugins-alphabetise

How to use it?

1. Alphabetical list of child pages using page title as the key:

  • A
    • Aa page
    • Ab page
  • B
    • Ba page
    • Bb page

In your template call, it like this:

<?php $alphabetise = alphabetise($page->children()->visible()->sortby('title'), array('key' => 'title')); ?>

The first argument you pass is the sorted page array you want to alphabetise. The second array key argument is so you can set what you want to alphabetise by. It should be a string like a page 'title'. The 'sortby' and the 'key' should usually be the same.

You then want to loop through the returned results and display them for example:

<?php foreach($alphabetise as $letter => $items): ?>
  <h4><?php echo strtoupper($letter) ?></h4>
  <ul>
      <?php foreach($items as $item): ?>
     	     <li>
     	         <a href="<?php echo $item->url()?>">
     	            <?php echo $item->title()?>
     	         </a>
     	     </li>
     	   <?php endforeach ?>
    </ul>
   <hr/>
<?php endforeach ?>

2. Alphabetical list of tags using tag name as the key:

  • A
    • Aa tag
    • Ab tag
  • B
    • Ba tag
    • Bb tag

You require the tag plugin for this bit to work! and 'pages' in this example are the pages you want to get tags for.
(See the taglcoud plugin documentation for more information.)

In your template call it like this:

<?php $alphabetise = alphabetise(tagcloud(($pages->find('pages')), array('sort' => 'name','sortdir'  => 'asc')), array('key' => 'name')); ?>

The first argument you pass is the tagcloud array you want to alphabetise. The second array key argument is so you can set what you want to alphabetise by. It should be a string like a tag 'name'.

You then want to loop through the returned results and display, note we are using $item->name not item->title as tags don't have titles, for example:

<?php foreach($alphabetise as $letter => $items): ?>
  <h4><?php echo strtoupper($letter) ?></h4>
    <ul>
      <?php foreach($items as $item): ?>
     	     <li>
     	         <a href="<?php echo $item->url()?>">
     	         <?php echo $item->name()?>
     	         </a>
     	     </li>
     	   <?php endforeach ?>
    </ul>
  <hr/>
<?php endforeach ?>

You can use any valid array element, so for tags you can use also add $item->results() for example, which is the number of items with that tag:

    <li>
        <a href="<?php echo $item->url()?>">
        <?php echo $item->name().' ('.($item->results()).')'?>
        </a>
    </li>

3. Set 'orderby' key:

Version 0.0.9 adds a key to alter how the array appears, the default is with letters and then numbers e.g.

  • A
  • B
  • 1
  • 2

Or you can not set the orderby key to SORT_STRING, to have the numbers listed first e.g.

  • 1
  • 2
  • A
  • B
<?php $alphabetise = alphabetise($page->children()->visible()->sortby('title'), array('key' => 'title', 'orderby'=>SORT_STRING))); ?>

Notes:

The array whose key your are trying to sort on should of course only contain letters of the alphabet, if not problems may occur.

Also the code (explode) uses a ~ tilde, if you use this in your key, especially at the beginning of the string, then you could run into sorting problems. You could of course manually change it if required.

We are using ksort so other sort_flags might be possible but not tested!

The orderby key is not a string!

Author

Russ Baldwin
shoesforindustry.net