Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- The `utils/fix-field-layout-uids` command now checks for duplicate top-level field layout UUIDs. ([#18193](https://github.com/craftcms/cms/pull/18193))
- 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))
- Fixed a bug where custom selects could be positioned incorrectly after the window was resized. ([#18179](https://github.com/craftcms/cms/issues/18179))

Expand Down
39 changes: 39 additions & 0 deletions src/console/controllers/utils/FixFieldLayoutUidsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class FixFieldLayoutUidsController extends Controller
{
private array $topLevelUids = [];

/**
* Fixes any duplicate UUIDs found within field layout components in the project config.
*
Expand All @@ -32,6 +34,8 @@ public function actionIndex(): int
$count = 0;
$this->_fixUids(Craft::$app->getProjectConfig()->get(), $count);

$this->fixFieldLayoutUids($count);

if ($count) {
$summary = sprintf('Fixed %s duplicate or missing %s.', $count, $count === 1 ? 'UUID' : 'UUIDs');
} else {
Expand All @@ -50,6 +54,7 @@ private function _fixUids(array $config, int &$count, string $path = '', array &
if (is_array($config['fieldLayouts'] ?? null)) {
$modified = false;
foreach ($config['fieldLayouts'] as $fieldLayoutUid => &$fieldLayoutConfig) {
$this->topLevelUids[$fieldLayoutUid][] = $path;
if (is_array($fieldLayoutConfig)) {
$fieldLayoutPath = sprintf('%sfieldLayouts.%s', $path ? "$path." : '', $fieldLayoutUid);
$this->_fixUidsInLayout($fieldLayoutConfig, $count, $fieldLayoutPath, $uids, $modified);
Expand Down Expand Up @@ -121,4 +126,38 @@ private function _checkUid(array &$config, int &$count, array &$uids, bool &$mod
$this->stdout($config['uid'], Console::FG_CYAN);
$this->stdout(".\n");
}

private function fixFieldLayoutUids(int &$count): void
{
foreach ($this->topLevelUids as $fieldLayoutUid => $paths) {
if (count($paths) == 1) {
unset($this->topLevelUids[$fieldLayoutUid]);
}
}

// we still have some duplicates remaining
if (!empty($this->topLevelUids)) {
foreach ($this->topLevelUids as $fieldLayoutUid => $paths) {
// leave the first path as is
array_shift($paths);
// all others need to have their UIDs adjusted
foreach ($paths as $path) {
$newUid = StringHelper::UUID();
$config = Craft::$app->getProjectConfig()->get($path);
$innerConfig = $config['fieldLayouts'][$fieldLayoutUid];
unset($config['fieldLayouts'][$fieldLayoutUid]);
$config['fieldLayouts'][$newUid] = $innerConfig;

$this->stdout(" > Duplicate UUID at ");
$this->stdout($path . ".fieldLayouts." . $fieldLayoutUid, Console::FG_CYAN);
$this->stdout(".\n Setting to ");
$this->stdout($newUid, Console::FG_CYAN);
$this->stdout(".\n");

Craft::$app->getProjectConfig()->set($path, $config);
$count++;
}
}
}
}
}