|
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('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('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) { |
| 36 | + // create a mapping: lowercase => original field name |
| 37 | + $lowercaseToOriginal = array_combine($contentFieldKeys, $originalContentKeys); |
34 | 38 |
|
35 | 39 | // 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 | + ); |
37 | 44 |
|
38 | 45 | // try to update the page with the data |
39 | | - $item->update($data, $lang); |
| 46 | + if (count($data) > 0) { |
| 47 | + $item->update($data, $lang); |
| 48 | + } |
40 | 49 | } |
41 | 50 | } |
42 | 51 | }; |
|
0 commit comments