From e87b102f503d330b6bddeabbece721e1c30b61e7 Mon Sep 17 00:00:00 2001 From: Voltra Date: Thu, 27 Jun 2024 18:56:17 +0000 Subject: [PATCH] Add a hook to allow mass-preload of related data when rendering a page's blocks --- .../views/components/page-blocks.blade.php | 23 +++++++++++++++---- src/Helpers.php | 23 +++++++++++++++++++ src/PageBlocks/PageBlock.php | 12 ++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 src/Helpers.php diff --git a/resources/views/components/page-blocks.blade.php b/resources/views/components/page-blocks.blade.php index fddfba3..fe2a37d 100644 --- a/resources/views/components/page-blocks.blade.php +++ b/resources/views/components/page-blocks.blade.php @@ -1,14 +1,27 @@ +@aware(['page']) @props(['blocks' => []]) +@php + $groups = \Z3d0X\FilamentFabricator\Helpers::arrayRefsGroupBy($blocks, 'type'); + + foreach ($groups as $blockType => &$group) { + /** + * @var class-string<\Z3d0X\FilamentFabricator\PageBlocks\PageBlock> $blockClass + */ + $blockClass = FilamentFabricator::getPageBlockFromName($blockType); + + if (!empty($blockClass)) { + $blockClass::preloadRelatedData($page, $group); + } + } +@endphp + @foreach ($blocks as $blockData) @php - $pageBlock = \Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getPageBlockFromName($blockData['type']) + $pageBlock = \Z3d0X\FilamentFabricator\Facades\FilamentFabricator::getPageBlockFromName($blockData['type']); @endphp @isset($pageBlock) - + @endisset @endforeach diff --git a/src/Helpers.php b/src/Helpers.php new file mode 100644 index 0000000..cca0ee2 --- /dev/null +++ b/src/Helpers.php @@ -0,0 +1,23 @@ +