diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e683a6ed54..ee1a12b723d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Fixed a bug where field conditions weren’t taking effect within Matrix fields set to inline-editable blocks mode, if the owner element didn’t support drafts. ([#16315](https://github.com/craftcms/cms/pull/16315)) +- Fixed a bug where Matrix fields’ entry types weren’t maintaining their original block type order when upgrading to Craft 5. ([#16314](https://github.com/craftcms/cms/issues/16314)) ## 5.5.6.1 - 2024-12-11 diff --git a/src/migrations/m230617_070415_entrify_matrix_blocks.php b/src/migrations/m230617_070415_entrify_matrix_blocks.php index ecc8766669d..2817754324f 100644 --- a/src/migrations/m230617_070415_entrify_matrix_blocks.php +++ b/src/migrations/m230617_070415_entrify_matrix_blocks.php @@ -15,6 +15,7 @@ use craft\models\EntryType; use craft\models\FieldLayout; use craft\services\ProjectConfig; +use Illuminate\Support\Arr; use yii\db\Exception as DbException; use yii\helpers\Inflector; @@ -90,8 +91,13 @@ public function safeUp(): bool foreach ($fieldConfigs as $fieldPath => $fieldConfig) { $fieldUid = ArrayHelper::lastValue(explode('.', $fieldPath)); $fieldEntryTypes = []; + $blockTypeConfigsByField[$fieldUid] ??= []; + $blockTypeConfigsByField[$fieldUid] = Arr::sort( + $blockTypeConfigsByField[$fieldUid], + fn(array $config) => $config['sortOrder'] ?? 0, + ); - foreach ($blockTypeConfigsByField[$fieldUid] ?? [] as $blockTypeUid => $blockTypeConfig) { + foreach ($blockTypeConfigsByField[$fieldUid] as $blockTypeUid => $blockTypeConfig) { $entryType = $newEntryTypes[] = $fieldEntryTypes[] = new EntryType([ 'uid' => $blockTypeUid, 'name' => $this->uniqueName($blockTypeConfig['name'], $entryTypeNames),