|
9 | 9 | array|null $ignore = null, |
10 | 10 | string|null $lang = null |
11 | 11 | ): void { |
12 | | - foreach($collection as $item) { |
| 12 | + foreach ($collection as $item) { |
13 | 13 | // get all fields in the content file |
14 | 14 | $contentFields = $item->content($lang)->fields(); |
15 | 15 |
|
|
20 | 20 | } |
21 | 21 | } |
22 | 22 |
|
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); |
25 | 26 |
|
26 | | - // get all field keys from blueprint |
| 27 | + // get all field keys from blueprint and normalize to lowercase |
27 | 28 | $blueprintFields = array_keys($item->blueprint()->fields()); |
| 29 | + $blueprintFieldKeys = array_map('mb_strtolower', $blueprintFields); |
28 | 30 |
|
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); |
31 | 33 |
|
32 | 34 | // update page only if there are any fields to be deleted |
33 | 35 | 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 | + } |
37 | 45 |
|
38 | 46 | // try to update the page with the data |
39 | 47 | $item->update($data, $lang); |
|
0 commit comments