]> BookStack Code Mirror - bookstack/blob - app/Entities/BreadcrumbsViewComposer.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Entities / BreadcrumbsViewComposer.php
1 <?php
2
3 namespace BookStack\Entities;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Tools\ShelfContext;
7 use Illuminate\View\View;
8
9 class BreadcrumbsViewComposer
10 {
11     public function __construct(
12         protected ShelfContext $shelfContext
13     ) {
14     }
15
16     /**
17      * Modify data when the view is composed.
18      */
19     public function compose(View $view): void
20     {
21         $crumbs = $view->getData()['crumbs'];
22         $firstCrumb = $crumbs[0] ?? null;
23
24         if ($firstCrumb instanceof Book) {
25             $shelf = $this->shelfContext->getContextualShelfForBook($firstCrumb);
26             if ($shelf) {
27                 array_unshift($crumbs, $shelf);
28                 $view->with('crumbs', $crumbs);
29             }
30         }
31     }
32 }