Skip to content

Commit 0b8e225

Browse files
authored
Merge pull request #18193 from craftcms/bugfix/fix-field-layout-uids-util-and-duplicate-layout-uids
ensure there's no field layouts with the same uid too
2 parents 98af957 + bcd1be4 commit 0b8e225

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- The `utils/fix-field-layout-uids` command now checks for duplicate top-level field layout UUIDs. ([#18193](https://github.com/craftcms/cms/pull/18193))
56
- Fixed a bug where all plugin settings were being saved to the project config, rather than just posted settings. ([craftcms/commerce#4006](https://github.com/craftcms/commerce/issues/4006))
67
- Fixed a bug where custom selects could be positioned incorrectly after the window was resized. ([#18179](https://github.com/craftcms/cms/issues/18179))
78

src/console/controllers/utils/FixFieldLayoutUidsController.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
class FixFieldLayoutUidsController extends Controller
2323
{
24+
private array $topLevelUids = [];
25+
2426
/**
2527
* Fixes any duplicate UUIDs found within field layout components in the project config.
2628
*
@@ -32,6 +34,8 @@ public function actionIndex(): int
3234
$count = 0;
3335
$this->_fixUids(Craft::$app->getProjectConfig()->get(), $count);
3436

37+
$this->fixFieldLayoutUids($count);
38+
3539
if ($count) {
3640
$summary = sprintf('Fixed %s duplicate or missing %s.', $count, $count === 1 ? 'UUID' : 'UUIDs');
3741
} else {
@@ -50,6 +54,7 @@ private function _fixUids(array $config, int &$count, string $path = '', array &
5054
if (is_array($config['fieldLayouts'] ?? null)) {
5155
$modified = false;
5256
foreach ($config['fieldLayouts'] as $fieldLayoutUid => &$fieldLayoutConfig) {
57+
$this->topLevelUids[$fieldLayoutUid][] = $path;
5358
if (is_array($fieldLayoutConfig)) {
5459
$fieldLayoutPath = sprintf('%sfieldLayouts.%s', $path ? "$path." : '', $fieldLayoutUid);
5560
$this->_fixUidsInLayout($fieldLayoutConfig, $count, $fieldLayoutPath, $uids, $modified);
@@ -121,4 +126,38 @@ private function _checkUid(array &$config, int &$count, array &$uids, bool &$mod
121126
$this->stdout($config['uid'], Console::FG_CYAN);
122127
$this->stdout(".\n");
123128
}
129+
130+
private function fixFieldLayoutUids(int &$count): void
131+
{
132+
foreach ($this->topLevelUids as $fieldLayoutUid => $paths) {
133+
if (count($paths) == 1) {
134+
unset($this->topLevelUids[$fieldLayoutUid]);
135+
}
136+
}
137+
138+
// we still have some duplicates remaining
139+
if (!empty($this->topLevelUids)) {
140+
foreach ($this->topLevelUids as $fieldLayoutUid => $paths) {
141+
// leave the first path as is
142+
array_shift($paths);
143+
// all others need to have their UIDs adjusted
144+
foreach ($paths as $path) {
145+
$newUid = StringHelper::UUID();
146+
$config = Craft::$app->getProjectConfig()->get($path);
147+
$innerConfig = $config['fieldLayouts'][$fieldLayoutUid];
148+
unset($config['fieldLayouts'][$fieldLayoutUid]);
149+
$config['fieldLayouts'][$newUid] = $innerConfig;
150+
151+
$this->stdout(" > Duplicate UUID at ");
152+
$this->stdout($path . ".fieldLayouts." . $fieldLayoutUid, Console::FG_CYAN);
153+
$this->stdout(".\n Setting to ");
154+
$this->stdout($newUid, Console::FG_CYAN);
155+
$this->stdout(".\n");
156+
157+
Craft::$app->getProjectConfig()->set($path, $config);
158+
$count++;
159+
}
160+
}
161+
}
162+
}
124163
}

0 commit comments

Comments
 (0)