Skip to content

Commit 6e5ca0d

Browse files
feat: overwriteContent option for resolved blocks
1 parent 9418cbb commit 6e5ca0d

File tree

4 files changed

+920
-162
lines changed

4 files changed

+920
-162
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
/index.dev.mjs
1313
/tests/coverage
1414
/tests/*/tmp
15+
/tools/**/vendor

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ return [
323323

324324
The `toResolvedBlocks()` method is a wrapper around the `toBlocks()` method. It is primarily intended for usage with KQL queries, because the `toBlocks()` method returns only UUIDs for the `files` and `pages` fields.
325325

326-
This field method will resolve the UUIDs to the actual file or page objects, so you can access their properties directly in your frontend.
326+
This field method will resolve the UUIDs to the actual file or page objects, so you can access their properties directly in your frontend. All resolved fields are stored in the `resolved` key of the block.
327327

328328
```php
329329
# /site/config/config.php
@@ -402,6 +402,19 @@ return [
402402
];
403403
```
404404

405+
#### Overwrite Blocks Content
406+
407+
By default, resolved fields don't overwrite the original field content. Instead, they are stored in the `resolved` key of the block. If you want to overwrite the original field content with the resolved fields, you can set the `overwriteContent` option to `true`:
408+
409+
```php
410+
# /site/config/config.php
411+
return [
412+
'blocksResolver' => [
413+
'overwriteContent' => true
414+
]
415+
];
416+
```
417+
405418
## Page Methods
406419

407420
### `i18nMeta()`

src/extensions/fieldMethods.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@
4848
// Get already resolved images
4949
$resolved = $block->content()->get('resolved')->or([])->value();
5050

51-
$block->content()->update([
52-
'resolved' => array_merge($resolved, [
53-
strtolower($key) => $images->map($defaultResolver)->values()
54-
])
55-
]);
51+
if ($kirby->option('blocksResolver.overwriteContent', false)) {
52+
$block->content()->update([
53+
$key => $images->map($defaultResolver)->values()
54+
]);
55+
} else {
56+
$block->content()->update([
57+
'resolved' => array_merge($resolved, [
58+
strtolower($key) => $images->map($defaultResolver)->values()
59+
])
60+
]);
61+
}
5662
}
5763

5864
return $block;
@@ -93,11 +99,17 @@
9399
// Get already resolved images
94100
$resolved = $block->content()->get('resolved')->or([])->value();
95101

96-
$block->content()->update([
97-
'resolved' => array_merge($resolved, [
98-
strtolower($key) => $pages->map($defaultResolver)->values()
99-
])
100-
]);
102+
if ($kirby->option('blocksResolver.overwriteContent', false)) {
103+
$block->content()->update([
104+
$key => $pages->map($defaultResolver)->values()
105+
]);
106+
} else {
107+
$block->content()->update([
108+
'resolved' => array_merge($resolved, [
109+
strtolower($key) => $pages->map($defaultResolver)->values()
110+
])
111+
]);
112+
}
101113
}
102114

103115
return $block;
@@ -115,14 +127,20 @@
115127
continue;
116128
}
117129

118-
$resolved = $block->content()->get('resolved')->or([])->value();
119130
$field = $block->content()->get($key);
131+
$resolved = $block->content()->get('resolved')->or([])->value();
120132

121-
$block->content()->update([
122-
'resolved' => array_merge($resolved, [
123-
strtolower($key) => $resolver($field, $block)
124-
])
125-
]);
133+
if ($kirby->option('blocksResolver.overwriteContent', false)) {
134+
$block->content()->update([
135+
$key => $resolver($field, $block)
136+
]);
137+
} else {
138+
$block->content()->update([
139+
'resolved' => array_merge($resolved, [
140+
strtolower($key) => $resolver($field, $block)
141+
])
142+
]);
143+
}
126144
}
127145

128146
return $block;

0 commit comments

Comments
 (0)