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