Skip to content

Commit d941683

Browse files
fix: resolve blocks in toResolvedLayouts correctly
1 parent a2aa4fd commit d941683

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/extensions/fieldMethods.php

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Kirby\Cms\Block;
44
use Kirby\Cms\File;
55
use Kirby\Cms\Layout;
6+
use Kirby\Cms\LayoutColumn;
67
use Kirby\Cms\Page;
78
use Kirby\Content\Field;
89
use Kirby\Exception\InvalidArgumentException;
@@ -185,36 +186,53 @@
185186
},
186187

187188
/**
188-
* Enhances the `toBlocks()` method to resolve files and pages
189+
* Enhances the `toBlocks()` method to resolve files, pages, and other fields
189190
*
190191
* @kql-allowed
191192
*/
192193
'toResolvedBlocks' => function (Field $field) use ($pagesFieldResolver, $filesFieldResolver, $customFieldResolver) {
193-
return $field
194-
->toBlocks()
194+
/** @var \Kirby\Cms\Blocks */
195+
$blocks = $field->toBlocks();
196+
return $blocks
195197
->map($pagesFieldResolver)
196198
->map($filesFieldResolver)
197199
->map($customFieldResolver);
198200
},
199201

200202
/**
201-
* Enhances the `toLayouts()` method to resolve files and pages
203+
* Enhances the `toLayouts()` method to resolve files, pages, and other fields
202204
*
203205
* @kql-allowed
204206
*/
205207
'toResolvedLayouts' => function (Field $field) use ($filesFieldResolver, $pagesFieldResolver, $customFieldResolver) {
206-
return $field
207-
->toLayouts()
208+
/** @var \Kirby\Cms\Layouts */
209+
$layouts = $field->toLayouts();
210+
return $layouts
208211
->map(function (Layout $layout) use ($filesFieldResolver, $pagesFieldResolver, $customFieldResolver) {
209-
foreach ($layout->columns() as $column) {
210-
$column
211-
->blocks()
212-
->map($filesFieldResolver)
213-
->map($pagesFieldResolver)
214-
->map($customFieldResolver);
215-
}
216-
217-
return $layout;
212+
$columns = $layout
213+
->columns()
214+
->map(function (LayoutColumn $column) use ($filesFieldResolver, $pagesFieldResolver, $customFieldResolver) {
215+
$blocks = $column
216+
->blocks()
217+
->map($filesFieldResolver)
218+
->map($pagesFieldResolver)
219+
->map($customFieldResolver);
220+
221+
return [
222+
'id' => $column->id(),
223+
'blocks' => $blocks->toArray(),
224+
'width' => $column->width()
225+
];
226+
});
227+
228+
return new Layout([
229+
'id' => $layout->id(),
230+
'field' => $layout->field(),
231+
'parent' => $layout->parent(),
232+
'siblings' => $layout->siblings(),
233+
'columns' => $columns->values(),
234+
'attrs' => $layout->attrs()->toArray()
235+
]);
218236
});
219237
}
220238
];

0 commit comments

Comments
 (0)