generated from filamentphp/plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a hook to allow mass-preload of related data when rendering a pag…
…e's blocks
- Loading branch information
Showing
3 changed files
with
53 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
<x-dynamic-component | ||
:component="$pageBlock::getComponent()" | ||
:attributes="new \Illuminate\View\ComponentAttributeBag($pageBlock::mutateData($blockData['data']))" | ||
/> | ||
<x-dynamic-component :component="$pageBlock::getComponent()" :attributes="new \Illuminate\View\ComponentAttributeBag($pageBlock::mutateData($blockData['data']))" /> | ||
@endisset | ||
@endforeach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Z3d0X\FilamentFabricator; | ||
|
||
abstract class Helpers | ||
{ | ||
/** | ||
* Group an array of associative arrays by a given key | ||
* | ||
* @param array[] $arr | ||
* @return array[] | ||
*/ | ||
public static function arrayRefsGroupBy(array &$arr, string $key): array | ||
{ | ||
$ret = []; | ||
|
||
foreach ($arr as &$item) { | ||
$ret[$item[$key]][] = &$item; | ||
} | ||
|
||
return $ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters