View Blocks: Added base implementation class

Also removed unused imports from app/ classes.
This commit is contained in:
Dan Brown
2026-09-21 13:03:18 +01:00
parent cf133540b1
commit 97fe7cca9d
51 changed files with 143 additions and 172 deletions
-2
View File
@@ -1,7 +1,5 @@
<?php
use Illuminate\Support\Str;
/**
* Caching configuration options.
*
@@ -13,7 +13,6 @@ use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Tools\BookContents;
use BookStack\Http\ApiController;
use BookStack\Permissions\Permission;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
@@ -23,7 +23,6 @@ use BookStack\Permissions\Permission;
use BookStack\Util\HtmlContentFilter;
use BookStack\Util\HtmlContentFilterConfig;
use Exception;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
@@ -11,7 +11,6 @@ use BookStack\Entities\Repos\DeletionRepo;
use BookStack\Http\ApiController;
use BookStack\Permissions\Permission;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
class RecycleBinApiController extends ApiController
{
-1
View File
@@ -10,7 +10,6 @@ use BookStack\Permissions\Models\JointPermission;
use BookStack\Permissions\PermissionApplicator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@@ -7,7 +7,6 @@ use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Entity;
use BookStack\Facades\Activity;
use BookStack\Permissions\Models\EntityPermission;
use BookStack\Permissions\Permission;
use BookStack\Users\Models\Role;
use BookStack\Users\Models\User;
@@ -12,7 +12,6 @@ use BookStack\Exports\ZipExports\Models\ZipExportChapter;
use BookStack\Exports\ZipExports\Models\ZipExportImage;
use BookStack\Exports\ZipExports\Models\ZipExportModel;
use BookStack\Exports\ZipExports\Models\ZipExportPage;
use BookStack\Permissions\Permission;
use BookStack\Uploads\Attachment;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageService;
-1
View File
@@ -2,7 +2,6 @@
namespace BookStack\Sorting;
use Closure;
use Illuminate\Support\Str;
enum SortRuleOperation: string
-1
View File
@@ -6,7 +6,6 @@ use BookStack\Access\SocialDriverManager;
use BookStack\Exceptions\ThemeException;
use Illuminate\Console\Application;
use Illuminate\Console\Application as Artisan;
use Illuminate\View\FileViewFinder;
use Symfony\Component\Console\Command\Command;
class ThemeService
@@ -13,7 +13,6 @@ use BookStack\Users\UserRepo;
use BookStack\View\ViewBlockManager;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
class UserAccountController extends Controller
@@ -2,7 +2,6 @@
namespace BookStack\Users\Controllers;
use BookStack\Entities\EntityExistsRule;
use BookStack\Exceptions\UserUpdateException;
use BookStack\Http\ApiController;
use BookStack\Permissions\Permission;
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace BookStack\View;
/**
* Base class for view blocks.
* This is intended as a base implementation for the ViewBlockInterface, and it's advised to extend
* this since we will aim to keep child implementations of this class forward compatible with future changes
* of the interface.
*/
abstract class BaseViewBlock implements ViewBlockInterface
{
}
@@ -2,7 +2,7 @@
namespace BookStack\View;
abstract class ViewBlock implements ViewBlockInterface
abstract class SimpleViewBlock extends BaseViewBlock
{
protected static string $id;
protected static string $view;
+4 -3
View File
@@ -16,14 +16,15 @@ interface ViewBlockInterface
/**
* Get the view to render for this block.
* Is provided with the existing parent view data.
* This is provided with the existing parent view data to allow the view used to be
* dynamic based on the context of its environment.
*/
public function getView(array $viewData): string;
/**
* Specify the data to pass to the view on render.
* Is provided with the existing parent view data and the original request.
* This is provided with the existing parent view data.
* @return array<string, mixed>
*/
public function withData(array $viewData): array;
public function getViewData(array $viewData): array;
}
+3 -4
View File
@@ -2,16 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksIndexActions extends ViewBlock
class BooksIndexActions extends SimpleViewBlock
{
protected static string $id = 'builtin_books-index-actions';
protected static string $view = 'books.parts.index-sidebar-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'view' => $viewData['view'],
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\BookQueries;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksIndexNew extends ViewBlock
class BooksIndexNew extends SimpleViewBlock
{
protected static string $id = 'builtin_books-index-new';
protected static string $view = 'books.parts.index-sidebar-section-new';
@@ -17,7 +16,7 @@ class BooksIndexNew extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$new = $this->queries->visibleForList()
->orderBy('created_at', 'desc')
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\BookQueries;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksIndexPopular extends ViewBlock
class BooksIndexPopular extends SimpleViewBlock
{
protected static string $id = 'builtin_books-index-popular';
protected static string $view = 'books.parts.index-sidebar-section-popular';
@@ -17,7 +16,7 @@ class BooksIndexPopular extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'popular' => $this->queries->popularForList()->take(4)->get(),
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\BookQueries;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksIndexRecents extends ViewBlock
class BooksIndexRecents extends SimpleViewBlock
{
protected static string $id = 'builtin_books-index-recents';
protected static string $view = 'books.parts.index-sidebar-section-recents';
@@ -17,7 +16,7 @@ class BooksIndexRecents extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$recents = null;
if (!user()->isGuest()) {
+3 -4
View File
@@ -4,16 +4,15 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksShowActions extends ViewBlock
class BooksShowActions extends SimpleViewBlock
{
protected static string $id = 'builtin_books-show-actions';
protected static string $view = 'books.parts.show-sidebar-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -4,10 +4,9 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Activity\ActivityQueries;
use BookStack\Entities\Models\Book;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksShowActivity extends ViewBlock
class BooksShowActivity extends SimpleViewBlock
{
protected static string $id = 'builtin_books-show-activity';
protected static string $view = 'books.parts.show-sidebar-section-activity';
@@ -18,7 +17,7 @@ class BooksShowActivity extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -5,10 +5,9 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
use BookStack\References\ReferenceFetcher;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksShowDetails extends ViewBlock
class BooksShowDetails extends SimpleViewBlock
{
protected static string $id = 'builtin_books-show-details';
protected static string $view = 'books.parts.show-sidebar-section-details';
@@ -19,7 +18,7 @@ class BooksShowDetails extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -2,16 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksShowSearchForm extends ViewBlock
class BooksShowSearchForm extends SimpleViewBlock
{
protected static string $id = 'builtin_books-show-search-form';
protected static string $view = 'entities.search-form';
protected static string $labelTranslationKey = 'common.search';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'label' => trans('entities.books_search_this'),
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Book;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksShowShelves extends ViewBlock
class BooksShowShelves extends SimpleViewBlock
{
protected static string $id = 'builtin_books-show-shelves';
protected static string $view = 'books.parts.show-sidebar-section-shelves';
protected static string $labelTranslationKey = 'entities.shelves';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -2,16 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class BooksShowTags extends ViewBlock
class BooksShowTags extends SimpleViewBlock
{
protected static string $id = 'builtin_books-show-tags';
protected static string $view = 'books.parts.show-sidebar-section-tags';
protected static string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'book' => $viewData['book'],
+3 -4
View File
@@ -4,16 +4,15 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Chapter;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ChaptersShowActions extends ViewBlock
class ChaptersShowActions extends SimpleViewBlock
{
protected static string $id = 'builtin_chapters-show-actions';
protected static string $view = 'chapters.parts.show-sidebar-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Chapter $chapter */
$chapter = $viewData['chapter'];
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Book;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ChaptersShowBookTree extends ViewBlock
class ChaptersShowBookTree extends SimpleViewBlock
{
protected static string $id = 'builtin_chapters-show-book-tree';
protected static string $view = 'entities.book-tree';
protected static string $labelTranslationKey = 'entities.books_navigation';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -6,10 +6,9 @@ use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\References\ReferenceFetcher;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ChaptersShowDetails extends ViewBlock
class ChaptersShowDetails extends SimpleViewBlock
{
protected static string $id = 'builtin_chapters-show-details';
protected static string $view = 'chapters.parts.show-sidebar-section-details';
@@ -20,7 +19,7 @@ class ChaptersShowDetails extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
@@ -2,16 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ChaptersShowSearchForm extends ViewBlock
class ChaptersShowSearchForm extends SimpleViewBlock
{
protected static string $id = 'builtin_chapters-show-search-form';
protected static string $view = 'entities.search-form';
protected static string $labelTranslationKey = 'common.search';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'label' => trans('entities.chapters_search_this'),
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Chapter;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ChaptersShowTags extends ViewBlock
class ChaptersShowTags extends SimpleViewBlock
{
protected static string $id = 'builtin_chapters-show-tags';
protected static string $view = 'chapters.parts.show-sidebar-section-tags';
protected static string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Chapter $chapter */
$chapter = $viewData['chapter'];
+3 -3
View File
@@ -2,15 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use BookStack\View\SimpleViewBlock;
class HomeActions extends ViewBlock
class HomeActions extends SimpleViewBlock
{
protected static string $id = 'builtin_home-actions';
protected static string $view = 'home.parts.configured-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'view' => $viewData['view'] ?? '',
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Activity\ActivityQueries;
use BookStack\View\ViewBlockInterface;
use Illuminate\Http\Request;
use BookStack\View\BaseViewBlock;
class HomeRecentActivity implements ViewBlockInterface
class HomeRecentActivity extends BaseViewBlock
{
public function __construct(
protected ActivityQueries $activityQueries
@@ -32,7 +31,7 @@ class HomeRecentActivity implements ViewBlockInterface
return 'home.parts.configured-section-recent-activity';
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$activity = $this->activityQueries->latest(10);
return [
+3 -3
View File
@@ -3,9 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\PageQueries;
use BookStack\View\ViewBlockInterface;
use BookStack\View\BaseViewBlock;
class HomeRecentDrafts implements ViewBlockInterface
class HomeRecentDrafts extends BaseViewBlock
{
public function __construct(
protected PageQueries $pageQueries
@@ -31,7 +31,7 @@ class HomeRecentDrafts implements ViewBlockInterface
return 'home.parts.configured-section-recent-drafts';
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$draftPages = [];
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\PageQueries;
use BookStack\View\ViewBlockInterface;
use Illuminate\Http\Request;
use BookStack\View\BaseViewBlock;
class HomeRecentlyUpdatedPages implements ViewBlockInterface
class HomeRecentlyUpdatedPages extends BaseViewBlock
{
public function __construct(
protected PageQueries $queries
@@ -32,7 +31,7 @@ class HomeRecentlyUpdatedPages implements ViewBlockInterface
return 'home.parts.configured-section-recently-updated-pages';
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$recentlyUpdatedPages = $this->queries->visibleForList()
->where('draft', false)
@@ -4,11 +4,9 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\EntityQueries;
use BookStack\Entities\Queries\QueryRecentlyViewed;
use BookStack\Entities\Queries\QueryTopFavourites;
use BookStack\View\ViewBlockInterface;
use Illuminate\Http\Request;
use BookStack\View\BaseViewBlock;
class HomeRecentlyViewedOrRecentBooks implements ViewBlockInterface
class HomeRecentlyViewedOrRecentBooks extends BaseViewBlock
{
public function __construct(
protected EntityQueries $queries,
@@ -36,7 +34,7 @@ class HomeRecentlyViewedOrRecentBooks implements ViewBlockInterface
return 'home.parts.configured-section-recently-viewed-or-recent-books';
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
if (user()->isGuest()) {
$recents = $this->queries->books->visibleForList()
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\QueryTopFavourites;
use BookStack\View\ViewBlockInterface;
use Illuminate\Http\Request;
use BookStack\View\BaseViewBlock;
class HomeTopFavourites implements ViewBlockInterface
class HomeTopFavourites extends BaseViewBlock
{
public function __construct(
protected QueryTopFavourites $topFavourites
@@ -32,7 +31,7 @@ class HomeTopFavourites implements ViewBlockInterface
return 'home.parts.configured-section-top-favourites';
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$favourites = $this->topFavourites->run(6);
return [
+3 -4
View File
@@ -4,16 +4,15 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Page;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class PagesShowActions extends ViewBlock
class PagesShowActions extends SimpleViewBlock
{
protected static string $id = 'builtin_pages-show-actions';
protected static string $view = 'pages.parts.show-sidebar-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Page $page */
$page = $viewData['page'];
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Page;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class PagesShowAttachments extends ViewBlock
class PagesShowAttachments extends SimpleViewBlock
{
protected static string $id = 'builtin_pages-show-attachments';
protected static string $view = 'pages.parts.show-sidebar-section-attachments';
protected static string $labelTranslationKey = 'entities.pages_attachments';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Page $page */
$page = $viewData['page'];
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Book;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class PagesShowBookTree extends ViewBlock
class PagesShowBookTree extends SimpleViewBlock
{
protected static string $id = 'builtin_pages-show-book-tree';
protected static string $view = 'entities.book-tree';
protected static string $labelTranslationKey = 'entities.books_navigation';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -6,10 +6,9 @@ use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Page;
use BookStack\References\ReferenceFetcher;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class PagesShowDetails extends ViewBlock
class PagesShowDetails extends SimpleViewBlock
{
protected static string $id = 'builtin_pages-show-details';
protected static string $view = 'pages.parts.show-sidebar-section-details';
@@ -20,7 +19,7 @@ class PagesShowDetails extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Book $book */
$book = $viewData['book'];
+3 -4
View File
@@ -4,16 +4,15 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Tools\PageContent;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class PagesShowPageNav extends ViewBlock
class PagesShowPageNav extends SimpleViewBlock
{
protected static string $id = 'builtin_pages-show-page-nav';
protected static string $view = 'pages.parts.show-sidebar-section-page-nav';
protected static string $labelTranslationKey = 'entities.pages_navigation';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Page $page */
$page = $viewData['page'];
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Page;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class PagesShowTags extends ViewBlock
class PagesShowTags extends SimpleViewBlock
{
protected static string $id = 'builtin_pages-show-tags';
protected static string $view = 'pages.parts.show-sidebar-section-tags';
protected static string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Page $page */
$page = $viewData['page'];
+3 -4
View File
@@ -2,16 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesIndexActions extends ViewBlock
class ShelvesIndexActions extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-index-actions';
protected static string $view = 'shelves.parts.index-sidebar-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'view' => $viewData['view'],
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesIndexNew extends ViewBlock
class ShelvesIndexNew extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-index-new';
protected static string $view = 'shelves.parts.index-sidebar-section-new';
@@ -17,7 +16,7 @@ class ShelvesIndexNew extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$new = $this->queries->visibleForList()
->orderBy('created_at', 'desc')
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesIndexPopular extends ViewBlock
class ShelvesIndexPopular extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-index-popular';
protected static string $view = 'shelves.parts.index-sidebar-section-popular';
@@ -17,7 +16,7 @@ class ShelvesIndexPopular extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'popular' => $this->queries->popularForList()->take(4)->get(),
+3 -4
View File
@@ -3,10 +3,9 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Queries\BookshelfQueries;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesIndexRecents extends ViewBlock
class ShelvesIndexRecents extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-index-recents';
protected static string $view = 'shelves.parts.index-sidebar-section-recents';
@@ -17,7 +16,7 @@ class ShelvesIndexRecents extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$recents = null;
if (!user()->isGuest()) {
+3 -4
View File
@@ -3,16 +3,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Bookshelf;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesShowActions extends ViewBlock
class ShelvesShowActions extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-show-actions';
protected static string $view = 'shelves.parts.show-sidebar-section-actions';
protected static string $labelTranslationKey = 'common.actions';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Bookshelf $shelf */
$shelf = $viewData['shelf'];
+3 -4
View File
@@ -4,10 +4,9 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Activity\ActivityQueries;
use BookStack\Entities\Models\Bookshelf;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesShowActivity extends ViewBlock
class ShelvesShowActivity extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-show-activity';
protected static string $view = 'shelves.parts.show-sidebar-section-activity';
@@ -18,7 +17,7 @@ class ShelvesShowActivity extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Bookshelf $shelf */
$shelf = $viewData['shelf'];
+3 -4
View File
@@ -4,10 +4,9 @@ namespace BookStack\View\ViewBlocks;
use BookStack\Entities\Models\Bookshelf;
use BookStack\References\ReferenceFetcher;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesShowDetails extends ViewBlock
class ShelvesShowDetails extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-show-details';
protected static string $view = 'shelves.parts.show-sidebar-section-details';
@@ -18,7 +17,7 @@ class ShelvesShowDetails extends ViewBlock
) {
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
/** @var Bookshelf $shelf */
$shelf = $viewData['shelf'];
+3 -4
View File
@@ -2,16 +2,15 @@
namespace BookStack\View\ViewBlocks;
use BookStack\View\ViewBlock;
use Illuminate\Http\Request;
use BookStack\View\SimpleViewBlock;
class ShelvesShowTags extends ViewBlock
class ShelvesShowTags extends SimpleViewBlock
{
protected static string $id = 'builtin_shelves-show-tags';
protected static string $view = 'shelves.parts.show-sidebar-section-tags';
protected static string $labelTranslationKey = 'entities.tags';
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
return [
'shelf' => $viewData['shelf'],
+19 -16
View File
@@ -203,14 +203,16 @@ or the cards shown on the home view.
As an example of using this system, we'll define a custom view block which will show on the default home view to state how many
books there are in the system.
To start, we'll need to create a custom class to represent our view block. This must implement `\BookStack\View\ViewBlockInterface`:
To start, we'll need to create a custom class to represent our view block.
This must implement `\BookStack\View\ViewBlockInterface`, but we'd advise extending `\BookStack\View\BaseViewBlock` since this will
be used as a core reference implementation, and is intended to be forward compatible with future changes:
```php
use BookStack\Entities\Queries\BookQueries;
use BookStack\View\ViewBlockInterface;
use BookStack\View\BaseViewBlock;
use BookStack\View\ViewBlockManager;
class BookTotalBlock implements ViewBlockInterface {
class BookTotalBlock extends BaseViewBlock {
public function __construct(
protected BookQueries $bookQueries
@@ -224,15 +226,15 @@ class BookTotalBlock implements ViewBlockInterface {
public static function getLabel(): string
{
return 'Total books displays';
return 'Total books display';
}
public function getView(array $viewData): string
{
return 'blocks.total-blocks';
return 'blocks.total-books';
}
public function withData(array $viewData): array
public function getViewData(array $viewData): array
{
$totalBooks = $this->bookQueries->visibleForList()->count();
return [
@@ -248,13 +250,14 @@ The interface has a few required methods:
- The `getLabel` method must provide a general string label for the block.
- The `getView` method provides a string path to a view file (Can be one custom registered).
- This is provided the available view data at time of render, so it can be dynamic based on context.
- The `withData` method is called when block is being rendered, and it should return an array of data which will be merged with existing view data.
- This is also provided available view data, for use as context.
- The `getViewData` method is called when the block is being rendered, and it should return an array of data which will be merged with existing view data.
- This is also provided with the available view data, for use as context.
In this example, we're also making use of the `BookQueries` internal BookStack class.
You're able to inject any other dependent classes/services via the constructor like this, and BookStack will attempt to auto-resolve them.
You're able to inject any other dependent classes/services via the constructor like this, and BookStack will attempt to auto-resolve them.
_Keep in mind that any used internal BookStack services or classes are not assured to be stable, so may receive breaking changes on update._
We can then register this block class using the logical theme system like so:
We can then register our custom block class using the logical theme system like so:
```php
use BookStack\Facades\Theme;
@@ -271,14 +274,14 @@ Theme::listen(ThemeEvents::VIEW_BLOCKS_REGISTER, function (ViewBlockManager $man
```
The above registration code would typically be within your `functions.php` theme file.
You could also define the above block class in the same file or separate it into its own file
You could also define the above block class in the same file, or separate it into its own class file
and include it from the `functions.php` via a `require_once()` call.
Lastly, we'll need to create the view for the registered block.
In our example we return `blocks.total-blocks` from the `getView` method, so our view needs to be located at
`blocks/total-blocks.blade.php` from a view providing directory.
In a theme folder, we can just create it at `blocks/total-blocks.blade.php` within the folder path.
If we were building in a module, we'd need to create this at `views/blocks/total-blocks.blade.php` within our module folder.
In our example we return `blocks.total-books` from the `getView` method, so our view needs to be located at
`blocks/total-books.blade.php` from a view providing directory.
In a theme folder, we can just create it at `blocks/total-books.blade.php` within the folder path.
If we were building in a module, we'd need to create this at `views/blocks/total-books.blade.php` within our module folder.
For our example, we'll use this content to make use of the data we're passing to the view:
```html
@@ -294,4 +297,4 @@ For our example, we'll use this content to make use of the data we're passing to
This will then show up on the default home grid view, in the right column.
The exact position within that location may change depending on user preferences, but the block will be limited
to the location provided during registration unless it has also been registered for other locations.
to the layout location provided during registration unless it has also been registered for other locations.
+1 -1
View File
@@ -6,5 +6,5 @@ $position - string
$sectionVars = get_defined_vars();
@endphp
@foreach($viewBlocks->getInstancesForLocationAndPositionForCurrentUser($location, $position) as $block)
@include($block->getView($sectionVars), $block->withData($sectionVars))
@include($block->getView($sectionVars), $block->getViewData($sectionVars))
@endforeach