Skip to content

Commit c13daff

Browse files
[6.x] Fix global set sites array being ordered by file modification time (#15298)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 25405b1 commit c13daff

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/UpdateScripts/UpdateGlobalVariables.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@ public function update()
3838
*/
3939
private function buildSitesArray(): void
4040
{
41-
GlobalSet::all()->each(function ($globalSet) {
41+
$siteOrder = Site::all()->keys()->flip();
42+
43+
GlobalSet::all()->each(function ($globalSet) use ($siteOrder) {
4244
$variables = GlobalVariables::whereSet($globalSet->handle());
4345

44-
$sites = $variables->mapWithKeys(function ($variable) {
45-
$contents = YAML::file($variable->path())->parse();
46-
$origin = Arr::get($contents, 'origin');
46+
$sites = $variables
47+
->sortBy(fn ($variable) => $siteOrder->get($variable->locale(), $siteOrder->count()))
48+
->mapWithKeys(function ($variable) {
49+
$contents = YAML::file($variable->path())->parse();
50+
$origin = Arr::get($contents, 'origin');
4751

48-
return [$variable->locale() => $origin];
49-
});
52+
return [$variable->locale() => $origin];
53+
});
5054

5155
$globalSet->sites($sites)->save();
5256

tests/UpdateScripts/UpdateGlobalVariablesTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,22 @@ public function it_builds_the_sites_array_in_a_multi_site_install()
120120
File::ensureDirectoryExists($this->globalsPath.'/de');
121121

122122
File::put($this->globalsPath.'/test.yaml', Yaml::dump(['title' => 'Test']));
123-
File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar', 'baz' => 'Qux']));
124-
File::put($this->globalsPath.'/fr/test.yaml', Yaml::dump(['origin' => 'en', 'foo' => 'Bar']));
123+
124+
// Written out of order on purpose. The Stache indexes variables by modification
125+
// time, but the sites array should follow the order of the sites config.
125126
File::put($this->globalsPath.'/de/test.yaml', Yaml::dump(['origin' => 'fr']));
127+
File::put($this->globalsPath.'/fr/test.yaml', Yaml::dump(['origin' => 'en', 'foo' => 'Bar']));
128+
File::put($this->globalsPath.'/en/test.yaml', Yaml::dump(['foo' => 'Bar', 'baz' => 'Qux']));
126129

127130
$this->runUpdateScript(UpdateGlobalVariables::class);
128131

129132
// Ensures that the sites array is built correctly.
130133
$expected = <<<'YAML'
131134
title: Test
132135
sites:
133-
de: fr
134136
en: null
135137
fr: en
138+
de: fr
136139

137140
YAML;
138141

0 commit comments

Comments
 (0)