Skip to content

Commit

Permalink
feat: resolve nested Kirby blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Mar 17, 2023
1 parent 3bb3c18 commit 74524c0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/extensions/fieldMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,37 @@
return $block;
};

$nestedBlocksResolver = function (\Kirby\Cms\Block $block) use ($imageBlockResolver) {
/** @var \Kirby\Cms\Block $block */
$nestedBlocks = option('headless.blocks.nested', ['prose']);
$blocksKeys = array_filter(
$block->content()->keys(),
fn ($i) => in_array($i, $nestedBlocks, true)
);

foreach ($blocksKeys as $key) {
/** @var \Kirby\Cms\Field $ktField */
$field = $block->content()->get($key);

$block->content()->update([
'prose' => $field->toBlocks()->map($imageBlockResolver)->toArray()
]);
}

return $block;
};

return [
/**
* Enhances the `toBlocks()` method to resolve images
*
* @kql-allowed
*/
'toResolvedBlocks' => function (\Kirby\Cms\Field $field) use ($imageBlockResolver) {
'toResolvedBlocks' => function (\Kirby\Cms\Field $field) use ($imageBlockResolver, $nestedBlocksResolver) {
return $field
->toBlocks()
->map($imageBlockResolver);
->map($imageBlockResolver)
->map($nestedBlocksResolver);
},

/**
Expand Down

0 comments on commit 74524c0

Please sign in to comment.