Updated autosort tagged books hack

Upon request via hack update service
This commit is contained in:
Dan Brown 2024-07-12 15:01:51 +01:00
commit 2d25a8b4f7
Signed by: danb
GPG key ID: 46D9F943C24A2EF9

View file

@ -1,6 +1,6 @@
<?php
use BookStack\Actions\ActivityType;
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
@ -11,7 +11,7 @@ use BookStack\Theming\ThemeEvents;
* Auto-sort the contents of the given book.
* This sorts in name order, ascending, with chapters first.
*/
function autoSortBook(Book $book) {
function autoSortBook(Book $book): void {
$chapters = $book->chapters()->orderBy('name', 'asc')->get(['id', 'priority']);
$pages = $book->pages()->orderBy('name', 'asc')
->where('draft', '=', false)
@ -51,5 +51,4 @@ Theme::listen(ThemeEvents::ACTIVITY_LOGGED, function (string $type, $detail) {
autoSortBook($detail->book);
}
}
});
});

View file

@ -2,11 +2,10 @@
title = "Autosort Tagged Books"
author = "@ssddanbrown"
date = 2023-01-23T20:00:00Z
updated = 2023-01-23T20:00:00Z
tested = "v22.11.1"
updated = 2024-07-12T14:00:00Z
tested = "v24.05.2"
+++
This is a hack to BookStack to enable auto-sorting of book chapters and pages upon page or chapter create/update. It sorts by name, ascending, with chapters first. By default it will run for any book with an `Autosort` tag assigned.
#### Options