]> BookStack Code Mirror - bookstack/blob - app/Entities/BreadcrumbsViewComposer.php
Added method for using enity ownership in relation queries
[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     protected $entityContextManager;
12
13     /**
14      * BreadcrumbsViewComposer constructor.
15      *
16      * @param ShelfContext $entityContextManager
17      */
18     public function __construct(ShelfContext $entityContextManager)
19     {
20         $this->entityContextManager = $entityContextManager;
21     }
22
23     /**
24      * Modify data when the view is composed.
25      *
26      * @param View $view
27      */
28     public function compose(View $view)
29     {
30         $crumbs = $view->getData()['crumbs'];
31         $firstCrumb = $crumbs[0] ?? null;
32         if ($firstCrumb instanceof Book) {
33             $shelf = $this->entityContextManager->getContextualShelfForBook($firstCrumb);
34             if ($shelf) {
35                 array_unshift($crumbs, $shelf);
36                 $view->with('crumbs', $crumbs);
37             }
38         }
39     }
40 }