From 183c48cdc9e29dae05ce88024ec5d418a22e3241 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Fri, 13 Dec 2024 10:32:35 -0800 Subject: [PATCH] Fixed #16314 --- CHANGELOG.md | 1 + src/migrations/m230617_070415_entrify_matrix_blocks.php | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) 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),