Skip to content

Commit b272dc8

Browse files
committed
fix: Fix case sensitivity in content field cleanup #94
Normalize field keys to lowercase when comparing content fields to blueprint fields, ensuring fields with case differences are properly detected and removed. This prevents orphaned fields due to case mismatches and improves data consistency.
1 parent 494ea08 commit b272dc8

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

commands/clean/content.php

Lines changed: 17 additions & 8 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,23 +20,32 @@
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('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('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) {
36+
// create a mapping: lowercase => original field name
37+
$lowercaseToOriginal = array_combine($contentFieldKeys, $originalContentKeys);
3438

3539
// flip keys and values and set new values to null
36-
$data = array_map(fn ($value) => null, array_flip($fieldsToBeDeleted));
40+
$data = array_map(
41+
fn ($field) => null,
42+
array_flip(array_intersect_key($lowercaseToOriginal, array_flip($fieldsToBeDeleted)))
43+
);
3744

3845
// try to update the page with the data
39-
$item->update($data, $lang);
46+
if (count($data) > 0) {
47+
$item->update($data, $lang);
48+
}
4049
}
4150
}
4251
};

0 commit comments

Comments
 (0)