namespace BookStack\Entities\Queries;
use BookStack\Activity\Models\View;
+use BookStack\Entities\Models\BookChild;
+use BookStack\Entities\Models\Entity;
+use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class Popular extends EntityQuery
$query->whereIn('viewable_type', $this->entityProvider()->getMorphClasses($filterModels));
}
- return $query->with('viewable')
+ $entities = $query->with('viewable')
->skip($count * ($page - 1))
->take($count)
->get()
->pluck('viewable')
->filter();
+
+ $this->loadBooksForChildren($entities);
+
+ return $entities;
+ }
+
+ protected function loadBooksForChildren(Collection $entities)
+ {
+ $bookChildren = $entities->filter(fn(Entity $entity) => $entity instanceof BookChild);
+ $eloquent = (new \Illuminate\Database\Eloquent\Collection($bookChildren));
+ $eloquent->load(['book' => function (BelongsTo $query) {
+ $query->scopes('visible');
+ }]);
}
}
class SearchController extends Controller
{
- protected SearchRunner $searchRunner;
-
- public function __construct(SearchRunner $searchRunner)
- {
- $this->searchRunner = $searchRunner;
+ public function __construct(
+ protected SearchRunner $searchRunner
+ ) {
}
/**
use BookStack\Activity\Models\Tag;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
+use BookStack\Entities\Models\Chapter;
use Tests\TestCase;
class EntitySearchTest extends TestCase
$chapterSearch->assertSee($chapter->book->getShortName(42));
}
+ public function test_entity_selector_shows_breadcrumbs_on_default_view()
+ {
+ $page = $this->entities->pageWithinChapter();
+ $this->asEditor()->get($page->chapter->getUrl());
+
+ $resp = $this->asEditor()->get('/search/entity-selector?types=book,chapter&permission=page-create');
+ $html = $this->withHtml($resp);
+ $html->assertElementContains('.chapter.entity-list-item', $page->chapter->name);
+ $html->assertElementContains('.chapter.entity-list-item .entity-item-snippet', $page->book->getShortName(42));
+ }
+
public function test_entity_selector_search_reflects_items_without_permission()
{
$page = $this->entities->page();