Skip to content

Commit e899579

Browse files
authored
Merge pull request #95 from getkirby/fix/94-clean-command
fix: Fix case sensitivity in content field cleanup
2 parents b263aa9 + 2998420 commit e899579

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

commands/clean/content.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
array|null $ignore = null,
1010
string|null $lang = null
1111
): void {
12-
foreach($collection as $item) {
12+
foreach ($collection as $item) {
1313
// get all fields in the content file
1414
$contentFields = $item->content($lang)->fields();
1515

@@ -20,20 +20,28 @@
2020
}
2121
}
2222

23-
// get the keys
24-
$contentFields = array_keys($contentFields);
23+
// get the keys and normalize to lowercase
24+
$originalContentKeys = array_keys($contentFields);
25+
$contentFieldKeys = array_map('mb_strtolower', $originalContentKeys);
2526

26-
// get all field keys from blueprint
27+
// get all field keys from blueprint and normalize to lowercase
2728
$blueprintFields = array_keys($item->blueprint()->fields());
29+
$blueprintFieldKeys = array_map('mb_strtolower', $blueprintFields);
2830

29-
// get all field keys that are in $contentFields but not in $blueprintFields
30-
$fieldsToBeDeleted = array_diff($contentFields, $blueprintFields);
31+
// get all field keys that are in $contentFieldKeys but not in $blueprintFieldKeys
32+
$fieldsToBeDeleted = array_diff($contentFieldKeys, $blueprintFieldKeys);
3133

3234
// update page only if there are any fields to be deleted
3335
if (count($fieldsToBeDeleted) > 0) {
34-
35-
// flip keys and values and set new values to null
36-
$data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted));
36+
// create a mapping: lowercase => original field name
37+
$lowercaseToOriginal = array_combine($contentFieldKeys, $originalContentKeys);
38+
39+
// build data array with original field names as keys and null as values
40+
$data = [];
41+
foreach ($fieldsToBeDeleted as $lowercaseField) {
42+
$originalField = $lowercaseToOriginal[$lowercaseField];
43+
$data[$originalField] = null;
44+
}
3745

3846
// try to update the page with the data
3947
$item->update($data, $lang);

0 commit comments

Comments
 (0)