These hacks are unsupported customizations meant as unofficial workarounds.
They can cause instability, introduce issues and may conflict with future updates.
Apply at your own risk!
This hack registers a custom view block which can be used on any type of home page.
This block lists total counts for shelves, books, chapters, pages and users in the system.
This was created to demonstrate custom view blocks on the release of BookStack v26.09,
as can be seen in the release video here.
Considerations
Only English text & labels are included.
All users will have access to this block.
Usage
It’s advised to install this as a theme module as guided below.
After installation, you’ll then find this block available as a section within the
“My Account > Interface Preferences > UI Layout Preferences > Homepage” view in BookStack.
By default it will show in the “Unused” area, but you can drag it into another column then save the layout.
Once configured, the totals should then show up on the homepage.
<?phpuseBookStack\Entities\Queries\EntityQueries;useBookStack\Facades\Theme;useBookStack\Theming\ThemeEvents;useBookStack\Users\Models\User;useBookStack\View\BaseViewBlock;useBookStack\View\ViewBlockManager;/**
* Define our custom block for displaying total stats.
* We extend the BaseViewBlock which implements the required ViewBlockInterface, and provides
* a basis which may help with forward compatibility if the interface changes in the future.
*/classTotalStatsBlockextendsBaseViewBlock{// Here we inject in the EntityQueries class, which BookStack will resolve automatically
// when creating the block instance.
publicfunction__construct(protectedEntityQueries$entityQueries,){}// We provide a unique ID for this block to distinguish it from other blocks.
// This is used as the key when the block is configured as part of a layout.
publicstaticfunctiongetId():string{return'com.bookstackapp.hacks.total-stats-block';}// We return the label which will show for this block in the configuration UI.
publicstaticfunctiongetLabel():string{return'Instance Total Stats';}// We return the view name which will be used to render the block.
// Here it's dynamic, based on the 'homeView' variable that's set when viewing home pages,
// so we can use different views depending on homepage type.
publicfunctiongetView(array$viewData):string{$viewName=($viewData['homeView']??'default')==='default'?'home-default':'home-non-default';return"custom-block-totals.{$viewName}";}// We return all the total counts, so that our custom block view can use this data.
publicfunctiongetViewData(array$viewData):array{return['totalUsers'=>User::query()->count(),'totalBooks'=>$this->entityQueries->books->visibleForList()->count(),'totalChapters'=>$this->entityQueries->chapters->visibleForList()->count(),'totalPages'=>$this->entityQueries->pages->visibleForList()->count(),'totalShelves'=>$this->entityQueries->shelves->visibleForList()->count(),];}}// We listen for the VIEW_BLOCKS_REGISTER event using the theme system which, when called,
// provides us a ViewBlockManager instance that we can use to register our custom block.
// We register the block twice, once for each homepage type.
Theme::listen(ThemeEvents::VIEW_BLOCKS_REGISTER,function(ViewBlockManager$manager){$manager->register('home-default','unused',TotalStatsBlock::class);$manager->register('home-non-default','unused',TotalStatsBlock::class);});
If using BookStack v26.03 or later, you can install this hack as
a theme module
on your BookStack instance by running the below artisan
command
from your BookStack installation folder:
Hack not working on the latest version of BookStack?
You can request this hack to be updated & tested for a small one-time fee.
This helps keeps these hacks updated & maintained in a sustainable manner.