]> BookStack Code Mirror - hacks/blob - content/page-include-tag-value/functions.php
Added page-include-tag-value hack
[hacks] / content / page-include-tag-value / functions.php
1 <?php
2
3 use BookStack\Entities\Models\Page;
4 use BookStack\Facades\Theme;
5 use BookStack\Theming\ThemeEvents;
6
7 // Listen to page include parsing events
8 Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, function (string $tagReference, string $replacementHTML, Page $currentPage, ?Page $referencedPage) {
9
10     // Allow default behaviour for non-tag-based includes
11     if (!str_starts_with($tagReference, '0tag:')) {
12          return null;
13     }
14
15     // Get the target tag name from the include reference
16     $tagName = explode(':', $tagReference)[1];
17
18     // Fetch the tag value from the page, parent chapter, or parent book
19     $tagValue = $currentPage->tags()->where('name', '=', $tagName)->first()?->value ??
20                 $currentPage->chapter?->tags()->where('name', '=', $tagName)->first()?->value ??
21                 $currentPage->book?->tags()->where('name', '=', $tagName)->first()?->value ?? '';
22
23     // Return the tag value to be used for the include
24     return $tagValue;
25 });