From 01b29515c8a73d30b1424c88d355fb08a2263106 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 16 Aug 2026 10:41:43 +0100 Subject: [PATCH] Layouts: Updated view block ids/labels to be static So that their metadata can be read without having to create instances and fullfill dependancy injection just to list. Also added title to interface prefs page. --- .../Controllers/UserAccountController.php | 2 + app/View/ViewBlock.php | 26 +++++------ app/View/ViewBlockInterface.php | 4 +- app/View/ViewBlockManager.php | 45 ++++++++++--------- app/View/ViewBlockPreferences.php | 6 +-- app/View/ViewBlocks/BooksIndexActions.php | 6 +-- app/View/ViewBlocks/BooksIndexNew.php | 6 +-- app/View/ViewBlocks/BooksIndexPopular.php | 6 +-- app/View/ViewBlocks/BooksIndexRecents.php | 6 +-- app/View/ViewBlocks/BooksShowActions.php | 6 +-- app/View/ViewBlocks/BooksShowActivity.php | 6 +-- app/View/ViewBlocks/BooksShowDetails.php | 6 +-- app/View/ViewBlocks/BooksShowSearchForm.php | 6 +-- app/View/ViewBlocks/BooksShowShelves.php | 6 +-- app/View/ViewBlocks/BooksShowTags.php | 6 +-- app/View/ViewBlocks/ChaptersShowActions.php | 6 +-- app/View/ViewBlocks/ChaptersShowBookTree.php | 6 +-- app/View/ViewBlocks/ChaptersShowDetails.php | 6 +-- .../ViewBlocks/ChaptersShowSearchForm.php | 6 +-- app/View/ViewBlocks/ChaptersShowTags.php | 6 +-- app/View/ViewBlocks/HomeActions.php | 6 +-- app/View/ViewBlocks/HomeRecentActivity.php | 4 +- app/View/ViewBlocks/HomeRecentDrafts.php | 4 +- .../ViewBlocks/HomeRecentlyUpdatedPages.php | 4 +- .../HomeRecentlyViewedOrRecentBooks.php | 4 +- app/View/ViewBlocks/HomeTopFavourites.php | 4 +- app/View/ViewBlocks/PagesShowActions.php | 6 +-- app/View/ViewBlocks/PagesShowAttachments.php | 6 +-- app/View/ViewBlocks/PagesShowBookTree.php | 6 +-- app/View/ViewBlocks/PagesShowDetails.php | 6 +-- app/View/ViewBlocks/PagesShowPageNav.php | 6 +-- app/View/ViewBlocks/PagesShowTags.php | 6 +-- app/View/ViewBlocks/ShelvesIndexActions.php | 6 +-- app/View/ViewBlocks/ShelvesIndexNew.php | 6 +-- app/View/ViewBlocks/ShelvesIndexPopular.php | 6 +-- app/View/ViewBlocks/ShelvesIndexRecents.php | 6 +-- app/View/ViewBlocks/ShelvesShowActions.php | 6 +-- app/View/ViewBlocks/ShelvesShowActivity.php | 6 +-- app/View/ViewBlocks/ShelvesShowDetails.php | 6 +-- app/View/ViewBlocks/ShelvesShowTags.php | 6 +-- resources/views/common/view-blocks.blade.php | 4 +- .../settings/layouts/parts/block.blade.php | 8 ++-- tests/View/ViewBlockDefaultsTest.php | 6 +-- 43 files changed, 151 insertions(+), 150 deletions(-) diff --git a/app/Users/Controllers/UserAccountController.php b/app/Users/Controllers/UserAccountController.php index 08c8c541b..71d8ca78d 100644 --- a/app/Users/Controllers/UserAccountController.php +++ b/app/Users/Controllers/UserAccountController.php @@ -165,6 +165,8 @@ class UserAccountController extends Controller */ public function showInterface(ViewBlockManager $viewBlockManager) { + $this->setPageTitle(trans('preferences.interface')); + return view('users.account.interface', [ 'category' => 'interface', 'namedLocations' => $viewBlockManager->getNamedLocations(), diff --git a/app/View/ViewBlock.php b/app/View/ViewBlock.php index beba07cb7..8907174bc 100644 --- a/app/View/ViewBlock.php +++ b/app/View/ViewBlock.php @@ -4,16 +4,24 @@ namespace BookStack\View; abstract class ViewBlock implements ViewBlockInterface { - protected string $id; - protected string $view; - protected string $labelTranslationKey; + protected static string $id; + protected static string $view; + protected static string $labelTranslationKey; /** * @inheritDoc */ - public function getId(): string + public static function getId(): string { - return $this->id; + return static::$id; + } + + /** + * @inheritDoc + */ + public static function getLabel(): string + { + return trans(static::$labelTranslationKey); } /** @@ -23,12 +31,4 @@ abstract class ViewBlock implements ViewBlockInterface { return $this->view; } - - /** - * @inheritDoc - */ - public function getLabel(): string - { - return trans($this->labelTranslationKey); - } } diff --git a/app/View/ViewBlockInterface.php b/app/View/ViewBlockInterface.php index 35dcb79fc..3d05d34d8 100644 --- a/app/View/ViewBlockInterface.php +++ b/app/View/ViewBlockInterface.php @@ -7,12 +7,12 @@ interface ViewBlockInterface /** * Get the unique ID for this block. */ - public function getId(): string; + public static function getId(): string; /** * Get the label for this block. */ - public function getLabel(): string; + public static function getLabel(): string; /** * Get the view to render for this block. diff --git a/app/View/ViewBlockManager.php b/app/View/ViewBlockManager.php index ac6da116b..f87d57d4b 100644 --- a/app/View/ViewBlockManager.php +++ b/app/View/ViewBlockManager.php @@ -44,46 +44,49 @@ class ViewBlockManager * @return ViewBlockInterface[] * @throws BindingResolutionException */ - public function getForLocationAndPositionForCurrentUser(string $location, string $position): array + public function getInstancesForLocationAndPositionForCurrentUser(string $location, string $position): array { $key = user()->id . ':' . $location; if (isset($this->locationBlockCache[$key])) { - return $this->locationBlockCache[$key][$position] ?? []; + $blocks = $this->locationBlockCache[$key][$position] ?? []; + return $this->blocksToInstances($blocks); } $forLocation = $this->getForLocationForCurrentUser($location); $this->locationBlockCache[$key] = $forLocation; - return $forLocation[$position] ?? []; + $blocks = $forLocation[$position] ?? []; + return $this->blocksToInstances($blocks); + } + + /** + * Create instances of the given block classes. + * @param class-string[] $blocks + * @return ViewBlockInterface[] + * @throws BindingResolutionException + */ + protected function blocksToInstances(array $blocks): array + { + return array_map(fn (string $blockClass) => app()->make($blockClass), $blocks); } /** * Get all blocks registered for a given location, as sets of arrays * keyed by position. - * @return array - * @throws BindingResolutionException + * @return array[]> */ - public function getForLocation(string $location): array + protected function getForLocation(string $location): array { $defaults = ViewBlockDefaults::getForLocation($location) ?? []; $registered = $this->blocksByLocationAndPosition[$location] ?? []; - $sections = array_merge_recursive($defaults, $registered); - - $instances = []; - foreach ($sections as $position => $blocks) { - $instances[$position] = array_map(function (string $className) { - return app()->make($className); - }, $blocks); - } - - return $instances; + return array_merge_recursive($defaults, $registered); } /** * Get all blocks registered for a given location, as sets of arrays * keyed by position, for the current user. * Same as above but with user-specific preferences applied. - * @return array + * @return array[]> * @throws BindingResolutionException */ public function getForLocationForCurrentUser(string $location): array @@ -160,7 +163,7 @@ class ViewBlockManager /** * Convert a blocksByPosition array into a map of block IDs to blocks. - * @param array $blocksByPosition + * @param array[]> $blocksByPosition * @return array */ protected function blocksByPositionToIdMap(array $blocksByPosition): array @@ -168,7 +171,7 @@ class ViewBlockManager $map = []; foreach ($blocksByPosition as $position => $blocks) { foreach ($blocks as $block) { - $map[$block->getId()] = $block; + $map[$block::getId()] = $block; } } return $map; @@ -176,7 +179,7 @@ class ViewBlockManager /** * Convert a blocksByPosition array into a map of block IDs to their positions. - * @param array $blocksByPosition + * @param array[]> $blocksByPosition * @return array */ protected function blocksByPositionToIdPositionMap(array $blocksByPosition): array @@ -184,7 +187,7 @@ class ViewBlockManager $map = []; foreach ($blocksByPosition as $position => $blocks) { foreach ($blocks as $block) { - $map[$block->getId()] = $position; + $map[$block::getId()] = $position; } } return $map; diff --git a/app/View/ViewBlockPreferences.php b/app/View/ViewBlockPreferences.php index ee965d5d1..77703068a 100644 --- a/app/View/ViewBlockPreferences.php +++ b/app/View/ViewBlockPreferences.php @@ -15,7 +15,7 @@ class ViewBlockPreferences * 'position-2' => ['block-id-3'], * ] * @param array $layoutData - * @param array $validBlocksByPosition + * @param array[]> $validBlocksByPosition * @throws BindingResolutionException */ public function storeByIdPositionMap( @@ -65,7 +65,7 @@ class ViewBlockPreferences } /** - * @param array $blocksByPosition + * @param array[]> $blocksByPosition * @return string[] */ protected function extractValidBlockIds(array $blocksByPosition): array @@ -74,7 +74,7 @@ class ViewBlockPreferences foreach ($blocksByPosition as $blocks) { foreach ($blocks as $block) { - $ids[] = $block->getId(); + $ids[] = $block::getId(); } } diff --git a/app/View/ViewBlocks/BooksIndexActions.php b/app/View/ViewBlocks/BooksIndexActions.php index 2d4f3df30..83327d63d 100644 --- a/app/View/ViewBlocks/BooksIndexActions.php +++ b/app/View/ViewBlocks/BooksIndexActions.php @@ -7,9 +7,9 @@ use Illuminate\Http\Request; class BooksIndexActions extends ViewBlock { - protected string $id = 'builtin_books-index-actions'; - protected string $view = 'books.parts.index-sidebar-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/BooksIndexNew.php b/app/View/ViewBlocks/BooksIndexNew.php index 620b650f9..daab45a46 100644 --- a/app/View/ViewBlocks/BooksIndexNew.php +++ b/app/View/ViewBlocks/BooksIndexNew.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class BooksIndexNew extends ViewBlock { - protected string $id = 'builtin_books-index-new'; - protected string $view = 'books.parts.index-sidebar-section-new'; - protected string $labelTranslationKey = 'entities.books_new'; + protected static string $id = 'builtin_books-index-new'; + protected static string $view = 'books.parts.index-sidebar-section-new'; + protected static string $labelTranslationKey = 'entities.books_new'; public function __construct( protected BookQueries $queries, diff --git a/app/View/ViewBlocks/BooksIndexPopular.php b/app/View/ViewBlocks/BooksIndexPopular.php index a7b2241cb..bfaee41e8 100644 --- a/app/View/ViewBlocks/BooksIndexPopular.php +++ b/app/View/ViewBlocks/BooksIndexPopular.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class BooksIndexPopular extends ViewBlock { - protected string $id = 'builtin_books-index-popular'; - protected string $view = 'books.parts.index-sidebar-section-popular'; - protected string $labelTranslationKey = 'entities.books_popular'; + protected static string $id = 'builtin_books-index-popular'; + protected static string $view = 'books.parts.index-sidebar-section-popular'; + protected static string $labelTranslationKey = 'entities.books_popular'; public function __construct( protected BookQueries $queries, diff --git a/app/View/ViewBlocks/BooksIndexRecents.php b/app/View/ViewBlocks/BooksIndexRecents.php index e4d76e23b..48abeb486 100644 --- a/app/View/ViewBlocks/BooksIndexRecents.php +++ b/app/View/ViewBlocks/BooksIndexRecents.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class BooksIndexRecents extends ViewBlock { - protected string $id = 'builtin_books-index-recents'; - protected string $view = 'books.parts.index-sidebar-section-recents'; - protected string $labelTranslationKey = 'entities.recently_viewed'; + protected static string $id = 'builtin_books-index-recents'; + protected static string $view = 'books.parts.index-sidebar-section-recents'; + protected static string $labelTranslationKey = 'entities.recently_viewed'; public function __construct( protected BookQueries $queries, diff --git a/app/View/ViewBlocks/BooksShowActions.php b/app/View/ViewBlocks/BooksShowActions.php index 946805c63..c7312f285 100644 --- a/app/View/ViewBlocks/BooksShowActions.php +++ b/app/View/ViewBlocks/BooksShowActions.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class BooksShowActions extends ViewBlock { - protected string $id = 'builtin_books-show-actions'; - protected string $view = 'books.parts.show-sidebar-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/BooksShowActivity.php b/app/View/ViewBlocks/BooksShowActivity.php index 7c723ffc0..14943bb8d 100644 --- a/app/View/ViewBlocks/BooksShowActivity.php +++ b/app/View/ViewBlocks/BooksShowActivity.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class BooksShowActivity extends ViewBlock { - protected string $id = 'builtin_books-show-activity'; - protected string $view = 'books.parts.show-sidebar-section-activity'; - protected string $labelTranslationKey = 'entities.recent_activity'; + protected static string $id = 'builtin_books-show-activity'; + protected static string $view = 'books.parts.show-sidebar-section-activity'; + protected static string $labelTranslationKey = 'entities.recent_activity'; public function __construct( protected ActivityQueries $activityQueries, diff --git a/app/View/ViewBlocks/BooksShowDetails.php b/app/View/ViewBlocks/BooksShowDetails.php index 5c690d613..582aec8b9 100644 --- a/app/View/ViewBlocks/BooksShowDetails.php +++ b/app/View/ViewBlocks/BooksShowDetails.php @@ -10,9 +10,9 @@ use Illuminate\Http\Request; class BooksShowDetails extends ViewBlock { - protected string $id = 'builtin_books-show-details'; - protected string $view = 'books.parts.show-sidebar-section-details'; - protected string $labelTranslationKey = 'common.details'; + protected static string $id = 'builtin_books-show-details'; + protected static string $view = 'books.parts.show-sidebar-section-details'; + protected static string $labelTranslationKey = 'common.details'; public function __construct( protected ReferenceFetcher $referenceFetcher, diff --git a/app/View/ViewBlocks/BooksShowSearchForm.php b/app/View/ViewBlocks/BooksShowSearchForm.php index 17e770e17..2fe099047 100644 --- a/app/View/ViewBlocks/BooksShowSearchForm.php +++ b/app/View/ViewBlocks/BooksShowSearchForm.php @@ -7,9 +7,9 @@ use Illuminate\Http\Request; class BooksShowSearchForm extends ViewBlock { - protected string $id = 'builtin_books-show-search-form'; - protected string $view = 'entities.search-form'; - protected string $labelTranslationKey = 'common.search'; + 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 { diff --git a/app/View/ViewBlocks/BooksShowShelves.php b/app/View/ViewBlocks/BooksShowShelves.php index 5a0424db5..590311c7f 100644 --- a/app/View/ViewBlocks/BooksShowShelves.php +++ b/app/View/ViewBlocks/BooksShowShelves.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class BooksShowShelves extends ViewBlock { - protected string $id = 'builtin_books-show-shelves'; - protected string $view = 'books.parts.show-sidebar-section-shelves'; - protected string $labelTranslationKey = 'entities.shelves'; + 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 { diff --git a/app/View/ViewBlocks/BooksShowTags.php b/app/View/ViewBlocks/BooksShowTags.php index 90de1e9c8..0448cd970 100644 --- a/app/View/ViewBlocks/BooksShowTags.php +++ b/app/View/ViewBlocks/BooksShowTags.php @@ -7,9 +7,9 @@ use Illuminate\Http\Request; class BooksShowTags extends ViewBlock { - protected string $id = 'builtin_books-show-tags'; - protected string $view = 'books.parts.show-sidebar-section-tags'; - protected string $labelTranslationKey = 'entities.tags'; + 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 { diff --git a/app/View/ViewBlocks/ChaptersShowActions.php b/app/View/ViewBlocks/ChaptersShowActions.php index 1f06a89c5..1beb9c591 100644 --- a/app/View/ViewBlocks/ChaptersShowActions.php +++ b/app/View/ViewBlocks/ChaptersShowActions.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class ChaptersShowActions extends ViewBlock { - protected string $id = 'builtin_chapters-show-actions'; - protected string $view = 'chapters.parts.show-sidebar-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/ChaptersShowBookTree.php b/app/View/ViewBlocks/ChaptersShowBookTree.php index a0bec8c7b..03f36862d 100644 --- a/app/View/ViewBlocks/ChaptersShowBookTree.php +++ b/app/View/ViewBlocks/ChaptersShowBookTree.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class ChaptersShowBookTree extends ViewBlock { - protected string $id = 'builtin_chapters-show-book-tree'; - protected string $view = 'entities.book-tree'; - protected string $labelTranslationKey = 'entities.books_navigation'; + 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 { diff --git a/app/View/ViewBlocks/ChaptersShowDetails.php b/app/View/ViewBlocks/ChaptersShowDetails.php index a65d17e2f..700b848f4 100644 --- a/app/View/ViewBlocks/ChaptersShowDetails.php +++ b/app/View/ViewBlocks/ChaptersShowDetails.php @@ -11,9 +11,9 @@ use Illuminate\Http\Request; class ChaptersShowDetails extends ViewBlock { - protected string $id = 'builtin_chapters-show-details'; - protected string $view = 'chapters.parts.show-sidebar-section-details'; - protected string $labelTranslationKey = 'common.details'; + protected static string $id = 'builtin_chapters-show-details'; + protected static string $view = 'chapters.parts.show-sidebar-section-details'; + protected static string $labelTranslationKey = 'common.details'; public function __construct( protected ReferenceFetcher $referenceFetcher, diff --git a/app/View/ViewBlocks/ChaptersShowSearchForm.php b/app/View/ViewBlocks/ChaptersShowSearchForm.php index 9443994bb..0014e30ea 100644 --- a/app/View/ViewBlocks/ChaptersShowSearchForm.php +++ b/app/View/ViewBlocks/ChaptersShowSearchForm.php @@ -7,9 +7,9 @@ use Illuminate\Http\Request; class ChaptersShowSearchForm extends ViewBlock { - protected string $id = 'builtin_chapters-show-search-form'; - protected string $view = 'entities.search-form'; - protected string $labelTranslationKey = 'common.search'; + 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 { diff --git a/app/View/ViewBlocks/ChaptersShowTags.php b/app/View/ViewBlocks/ChaptersShowTags.php index fd42283cb..e513ee66b 100644 --- a/app/View/ViewBlocks/ChaptersShowTags.php +++ b/app/View/ViewBlocks/ChaptersShowTags.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class ChaptersShowTags extends ViewBlock { - protected string $id = 'builtin_chapters-show-tags'; - protected string $view = 'chapters.parts.show-sidebar-section-tags'; - protected string $labelTranslationKey = 'entities.tags'; + 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 { diff --git a/app/View/ViewBlocks/HomeActions.php b/app/View/ViewBlocks/HomeActions.php index 94788c522..211a60df5 100644 --- a/app/View/ViewBlocks/HomeActions.php +++ b/app/View/ViewBlocks/HomeActions.php @@ -6,9 +6,9 @@ use BookStack\View\ViewBlock; class HomeActions extends ViewBlock { - protected string $id = 'builtin_home-actions'; - protected string $view = 'home.parts.configured-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/HomeRecentActivity.php b/app/View/ViewBlocks/HomeRecentActivity.php index b5b252fbf..06869990f 100644 --- a/app/View/ViewBlocks/HomeRecentActivity.php +++ b/app/View/ViewBlocks/HomeRecentActivity.php @@ -13,12 +13,12 @@ class HomeRecentActivity implements ViewBlockInterface ) { } - public function getId(): string + public static function getId(): string { return 'builtin_home-recent-activity'; } - public function getLabel(): string + public static function getLabel(): string { return trans('entities.recent_activity'); } diff --git a/app/View/ViewBlocks/HomeRecentDrafts.php b/app/View/ViewBlocks/HomeRecentDrafts.php index 458353a57..dc8892513 100644 --- a/app/View/ViewBlocks/HomeRecentDrafts.php +++ b/app/View/ViewBlocks/HomeRecentDrafts.php @@ -12,12 +12,12 @@ class HomeRecentDrafts implements ViewBlockInterface ) { } - public function getId(): string + public static function getId(): string { return 'builtin_home-recent-drafts'; } - public function getLabel(): string + public static function getLabel(): string { return trans('entities.my_recent_drafts'); } diff --git a/app/View/ViewBlocks/HomeRecentlyUpdatedPages.php b/app/View/ViewBlocks/HomeRecentlyUpdatedPages.php index 032ef6b46..99bec7928 100644 --- a/app/View/ViewBlocks/HomeRecentlyUpdatedPages.php +++ b/app/View/ViewBlocks/HomeRecentlyUpdatedPages.php @@ -13,12 +13,12 @@ class HomeRecentlyUpdatedPages implements ViewBlockInterface ) { } - public function getId(): string + public static function getId(): string { return 'builtin_home-recently-updated-pages'; } - public function getLabel(): string + public static function getLabel(): string { return trans('entities.recently_updated_pages'); } diff --git a/app/View/ViewBlocks/HomeRecentlyViewedOrRecentBooks.php b/app/View/ViewBlocks/HomeRecentlyViewedOrRecentBooks.php index 2259b3513..64cb897ef 100644 --- a/app/View/ViewBlocks/HomeRecentlyViewedOrRecentBooks.php +++ b/app/View/ViewBlocks/HomeRecentlyViewedOrRecentBooks.php @@ -16,12 +16,12 @@ class HomeRecentlyViewedOrRecentBooks implements ViewBlockInterface ) { } - public function getId(): string + public static function getId(): string { return 'builtin_home-recently-viewed-or-recent-books'; } - public function getLabel(): string + public static function getLabel(): string { $key = user()->isGuest() ? 'books_recent' : 'my_recently_viewed'; return trans("entities.{$key}"); diff --git a/app/View/ViewBlocks/HomeTopFavourites.php b/app/View/ViewBlocks/HomeTopFavourites.php index b1b929f03..28eeed15d 100644 --- a/app/View/ViewBlocks/HomeTopFavourites.php +++ b/app/View/ViewBlocks/HomeTopFavourites.php @@ -13,12 +13,12 @@ class HomeTopFavourites implements ViewBlockInterface ) { } - public function getId(): string + public static function getId(): string { return 'builtin_home-top-favourites'; } - public function getLabel(): string + public static function getLabel(): string { return trans('entities.my_most_viewed_favourites'); } diff --git a/app/View/ViewBlocks/PagesShowActions.php b/app/View/ViewBlocks/PagesShowActions.php index 62b814926..06a184fa3 100644 --- a/app/View/ViewBlocks/PagesShowActions.php +++ b/app/View/ViewBlocks/PagesShowActions.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class PagesShowActions extends ViewBlock { - protected string $id = 'builtin_pages-show-actions'; - protected string $view = 'pages.parts.show-sidebar-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/PagesShowAttachments.php b/app/View/ViewBlocks/PagesShowAttachments.php index a50123c07..d29e9e9a6 100644 --- a/app/View/ViewBlocks/PagesShowAttachments.php +++ b/app/View/ViewBlocks/PagesShowAttachments.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class PagesShowAttachments extends ViewBlock { - protected string $id = 'builtin_pages-show-attachments'; - protected string $view = 'pages.parts.show-sidebar-section-attachments'; - protected string $labelTranslationKey = 'entities.pages_attachments'; + 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 { diff --git a/app/View/ViewBlocks/PagesShowBookTree.php b/app/View/ViewBlocks/PagesShowBookTree.php index c09285c71..4f5b6af6c 100644 --- a/app/View/ViewBlocks/PagesShowBookTree.php +++ b/app/View/ViewBlocks/PagesShowBookTree.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class PagesShowBookTree extends ViewBlock { - protected string $id = 'builtin_pages-show-book-tree'; - protected string $view = 'entities.book-tree'; - protected string $labelTranslationKey = 'entities.books_navigation'; + 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 { diff --git a/app/View/ViewBlocks/PagesShowDetails.php b/app/View/ViewBlocks/PagesShowDetails.php index 098baac5f..6bbe9a8b2 100644 --- a/app/View/ViewBlocks/PagesShowDetails.php +++ b/app/View/ViewBlocks/PagesShowDetails.php @@ -11,9 +11,9 @@ use Illuminate\Http\Request; class PagesShowDetails extends ViewBlock { - protected string $id = 'builtin_pages-show-details'; - protected string $view = 'pages.parts.show-sidebar-section-details'; - protected string $labelTranslationKey = 'common.details'; + protected static string $id = 'builtin_pages-show-details'; + protected static string $view = 'pages.parts.show-sidebar-section-details'; + protected static string $labelTranslationKey = 'common.details'; public function __construct( protected ReferenceFetcher $referenceFetcher, diff --git a/app/View/ViewBlocks/PagesShowPageNav.php b/app/View/ViewBlocks/PagesShowPageNav.php index 952bc9b8c..2a013ec2f 100644 --- a/app/View/ViewBlocks/PagesShowPageNav.php +++ b/app/View/ViewBlocks/PagesShowPageNav.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class PagesShowPageNav extends ViewBlock { - protected string $id = 'builtin_pages-show-page-nav'; - protected string $view = 'pages.parts.show-sidebar-section-page-nav'; - protected string $labelTranslationKey = 'entities.pages_navigation'; + 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 { diff --git a/app/View/ViewBlocks/PagesShowTags.php b/app/View/ViewBlocks/PagesShowTags.php index d8a9a8c7a..3edbb3288 100644 --- a/app/View/ViewBlocks/PagesShowTags.php +++ b/app/View/ViewBlocks/PagesShowTags.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class PagesShowTags extends ViewBlock { - protected string $id = 'builtin_pages-show-tags'; - protected string $view = 'pages.parts.show-sidebar-section-tags'; - protected string $labelTranslationKey = 'entities.tags'; + 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 { diff --git a/app/View/ViewBlocks/ShelvesIndexActions.php b/app/View/ViewBlocks/ShelvesIndexActions.php index 63c0ae344..8cd0f47d3 100644 --- a/app/View/ViewBlocks/ShelvesIndexActions.php +++ b/app/View/ViewBlocks/ShelvesIndexActions.php @@ -7,9 +7,9 @@ use Illuminate\Http\Request; class ShelvesIndexActions extends ViewBlock { - protected string $id = 'builtin_shelves-index-actions'; - protected string $view = 'shelves.parts.index-sidebar-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/ShelvesIndexNew.php b/app/View/ViewBlocks/ShelvesIndexNew.php index 28d5771ff..21f21a4cf 100644 --- a/app/View/ViewBlocks/ShelvesIndexNew.php +++ b/app/View/ViewBlocks/ShelvesIndexNew.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class ShelvesIndexNew extends ViewBlock { - protected string $id = 'builtin_shelves-index-new'; - protected string $view = 'shelves.parts.index-sidebar-section-new'; - protected string $labelTranslationKey = 'entities.shelves_new'; + protected static string $id = 'builtin_shelves-index-new'; + protected static string $view = 'shelves.parts.index-sidebar-section-new'; + protected static string $labelTranslationKey = 'entities.shelves_new'; public function __construct( protected BookshelfQueries $queries, diff --git a/app/View/ViewBlocks/ShelvesIndexPopular.php b/app/View/ViewBlocks/ShelvesIndexPopular.php index 454759815..df62f7c59 100644 --- a/app/View/ViewBlocks/ShelvesIndexPopular.php +++ b/app/View/ViewBlocks/ShelvesIndexPopular.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class ShelvesIndexPopular extends ViewBlock { - protected string $id = 'builtin_shelves-index-popular'; - protected string $view = 'shelves.parts.index-sidebar-section-popular'; - protected string $labelTranslationKey = 'entities.shelves_popular'; + protected static string $id = 'builtin_shelves-index-popular'; + protected static string $view = 'shelves.parts.index-sidebar-section-popular'; + protected static string $labelTranslationKey = 'entities.shelves_popular'; public function __construct( protected BookshelfQueries $queries, diff --git a/app/View/ViewBlocks/ShelvesIndexRecents.php b/app/View/ViewBlocks/ShelvesIndexRecents.php index 8025bf3ea..5f42d395c 100644 --- a/app/View/ViewBlocks/ShelvesIndexRecents.php +++ b/app/View/ViewBlocks/ShelvesIndexRecents.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class ShelvesIndexRecents extends ViewBlock { - protected string $id = 'builtin_shelves-index-recents'; - protected string $view = 'shelves.parts.index-sidebar-section-recents'; - protected string $labelTranslationKey = 'entities.recently_viewed'; + protected static string $id = 'builtin_shelves-index-recents'; + protected static string $view = 'shelves.parts.index-sidebar-section-recents'; + protected static string $labelTranslationKey = 'entities.recently_viewed'; public function __construct( protected BookshelfQueries $queries, diff --git a/app/View/ViewBlocks/ShelvesShowActions.php b/app/View/ViewBlocks/ShelvesShowActions.php index dd8a22bd0..08a3cdee1 100644 --- a/app/View/ViewBlocks/ShelvesShowActions.php +++ b/app/View/ViewBlocks/ShelvesShowActions.php @@ -8,9 +8,9 @@ use Illuminate\Http\Request; class ShelvesShowActions extends ViewBlock { - protected string $id = 'builtin_shelves-show-actions'; - protected string $view = 'shelves.parts.show-sidebar-section-actions'; - protected string $labelTranslationKey = 'common.actions'; + 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 { diff --git a/app/View/ViewBlocks/ShelvesShowActivity.php b/app/View/ViewBlocks/ShelvesShowActivity.php index 0d254bb3e..a14dbed5f 100644 --- a/app/View/ViewBlocks/ShelvesShowActivity.php +++ b/app/View/ViewBlocks/ShelvesShowActivity.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class ShelvesShowActivity extends ViewBlock { - protected string $id = 'builtin_shelves-show-activity'; - protected string $view = 'shelves.parts.show-sidebar-section-activity'; - protected string $labelTranslationKey = 'entities.recent_activity'; + protected static string $id = 'builtin_shelves-show-activity'; + protected static string $view = 'shelves.parts.show-sidebar-section-activity'; + protected static string $labelTranslationKey = 'entities.recent_activity'; public function __construct( protected ActivityQueries $activityQueries, diff --git a/app/View/ViewBlocks/ShelvesShowDetails.php b/app/View/ViewBlocks/ShelvesShowDetails.php index 31d7c6b4b..1ceb21b6f 100644 --- a/app/View/ViewBlocks/ShelvesShowDetails.php +++ b/app/View/ViewBlocks/ShelvesShowDetails.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class ShelvesShowDetails extends ViewBlock { - protected string $id = 'builtin_shelves-show-details'; - protected string $view = 'shelves.parts.show-sidebar-section-details'; - protected string $labelTranslationKey = 'common.details'; + protected static string $id = 'builtin_shelves-show-details'; + protected static string $view = 'shelves.parts.show-sidebar-section-details'; + protected static string $labelTranslationKey = 'common.details'; public function __construct( protected ReferenceFetcher $referenceFetcher, diff --git a/app/View/ViewBlocks/ShelvesShowTags.php b/app/View/ViewBlocks/ShelvesShowTags.php index 105e1c22b..37bc4b471 100644 --- a/app/View/ViewBlocks/ShelvesShowTags.php +++ b/app/View/ViewBlocks/ShelvesShowTags.php @@ -7,9 +7,9 @@ use Illuminate\Http\Request; class ShelvesShowTags extends ViewBlock { - protected string $id = 'builtin_shelves-show-tags'; - protected string $view = 'shelves.parts.show-sidebar-section-tags'; - protected string $labelTranslationKey = 'entities.tags'; + 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 { diff --git a/resources/views/common/view-blocks.blade.php b/resources/views/common/view-blocks.blade.php index 31d854286..7d26e38b4 100644 --- a/resources/views/common/view-blocks.blade.php +++ b/resources/views/common/view-blocks.blade.php @@ -5,6 +5,6 @@ $position - string @php $sectionVars = get_defined_vars(); @endphp -@foreach($viewBlocks->getForLocationAndPositionForCurrentUser($location, $position) as $section) - @include($section->getView($sectionVars), $section->withData($sectionVars)) +@foreach($viewBlocks->getInstancesForLocationAndPositionForCurrentUser($location, $position) as $block) + @include($block->getView($sectionVars), $block->withData($sectionVars)) @endforeach \ No newline at end of file diff --git a/resources/views/settings/layouts/parts/block.blade.php b/resources/views/settings/layouts/parts/block.blade.php index 398496ec0..9b36ee290 100644 --- a/resources/views/settings/layouts/parts/block.blade.php +++ b/resources/views/settings/layouts/parts/block.blade.php @@ -1,10 +1,8 @@ -{{-- -$block - ViewBlockInterface - View Block instance to show ---}} -
  • */ @endphp +
  • @icon('grip')
    -
    {{ $block->getLabel() }}
    +
    {{ $block::getLabel() }}
    diff --git a/tests/View/ViewBlockDefaultsTest.php b/tests/View/ViewBlockDefaultsTest.php index a19ba3629..6b47bcea7 100644 --- a/tests/View/ViewBlockDefaultsTest.php +++ b/tests/View/ViewBlockDefaultsTest.php @@ -22,13 +22,11 @@ class ViewBlockDefaultsTest extends TestCase $blocks = array_unique($classes); foreach ($blocks as $block) { - $instance = app()->make($block); - - $id = $instance->getId(); + $id = $block::getId(); $this->assertArrayNotHasKey($id, $trueById); $trueById[$id] = true; - $label = $instance->getLabel(); + $label = $block::getLabel(); $this->assertIsString($label); $this->assertDoesNotMatchRegularExpression('/^[a-z]{1,10}\./', $label); }