diff --git a/src/Extension/FluentExtension.php b/src/Extension/FluentExtension.php index 627f0fcb..88b46c44 100644 --- a/src/Extension/FluentExtension.php +++ b/src/Extension/FluentExtension.php @@ -981,10 +981,14 @@ protected function getRecordLocale() /** * Returns the source locale that will display the content for this record + * Source locale for frontend context is used by default as this is the most common use, + * but you can optionally specify CMS context as well + * Passing null will fall back to whatever context is currently in use * + * @param bool|null $isFrontend * @return Locale|null */ - public function getSourceLocale(): ?Locale + public function getSourceLocale(?bool $isFrontend = true): ?Locale { $owner = $this->owner; $currentLocale = FluentState::singleton()->getLocale(); @@ -995,7 +999,7 @@ public function getSourceLocale(): ?Locale } $localeInformation = $owner->LocaleInformation($currentLocale); - $sourceLocale = $localeInformation->getSourceLocale(); + $sourceLocale = $localeInformation->getSourceLocale($isFrontend); if ($sourceLocale) { return Locale::getByLocale($sourceLocale); } diff --git a/src/Model/RecordLocale.php b/src/Model/RecordLocale.php index 74f3a4f0..b21fa432 100644 --- a/src/Model/RecordLocale.php +++ b/src/Model/RecordLocale.php @@ -362,16 +362,23 @@ public function getStagesDiffer(): bool /** * Get the locale which is the source of content for this record + * Source locale for frontend context is used by default as this is the most common use, + * but you can optionally specify CMS context as well + * Passing null will fall back to whatever context is currently in use * + * @param bool|null $isFrontend * @return Locale|null */ - public function getSourceLocale(): ?Locale + public function getSourceLocale(?bool $isFrontend = true): ?Locale { + $isFrontendFromState = FluentState::singleton()->getIsFrontend(); + $isFrontend ??= $isFrontendFromState; + /** @var DataObject|FluentExtension $record */ $record = $this->getOriginalRecord(); $config = $record->config(); - $inheritanceMode = FluentState::singleton()->getIsFrontend() + $inheritanceMode = $isFrontend ? $config->get('frontend_publish_required') : $config->get('cms_localisation_required'); diff --git a/tests/php/Extension/LocaleInheritanceTest.php b/tests/php/Extension/LocaleInheritanceTest.php index ce2e8fed..2b617887 100644 --- a/tests/php/Extension/LocaleInheritanceTest.php +++ b/tests/php/Extension/LocaleInheritanceTest.php @@ -5,22 +5,11 @@ use Page; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Dev\SapphireTest; -use SilverStripe\ORM\DataObject; -use SilverStripe\ORM\Queries\SQLSelect; use SilverStripe\ORM\ValidationException; use SilverStripe\Versioned\Versioned; use TractorCow\Fluent\Extension\FluentExtension; use TractorCow\Fluent\Extension\FluentSiteTreeExtension; -use TractorCow\Fluent\Model\Locale; use TractorCow\Fluent\State\FluentState; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedAnother; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedChild; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\LocalisedParent; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\MixedLocalisedSortObject; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\TestModel; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\TestRelationPage; -use TractorCow\Fluent\Tests\Extension\FluentExtensionTest\UnlocalisedChild; -use TractorCow\Fluent\Tests\Extension\Stub\FluentStubObject; class LocaleInheritanceTest extends SapphireTest { @@ -60,7 +49,9 @@ public function testGetSourceLocale( // Create the page in the default locale FluentState::singleton()->withState( function (FluentState $state) use ($frontendContext, $locale, $expected): void { - $state->setLocale('en_US'); + $state + ->setLocale('en_US') + ->setIsFrontend($frontendContext); /** @var Page|FluentExtension $page */ $page = Page::create(); @@ -68,10 +59,8 @@ function (FluentState $state) use ($frontendContext, $locale, $expected): void { $page->URLSegment = 'test-page'; $page->write(); - $state->setIsFrontend($frontendContext); - $localeInformation = $page->LocaleInformation($locale); - $sourceLocaleObject = $localeInformation->getSourceLocale(); + $sourceLocaleObject = $localeInformation->getSourceLocale($frontendContext); $sourceLocale = $sourceLocaleObject?->Locale; $this->assertEquals( $expected, @@ -90,7 +79,7 @@ function (FluentState $state) use ($frontendContext, $locale, $expected): void { $page = Page::get()->byID($page->ID); $this->assertNotNull($page, 'We expect the page to be available in this locale'); - $sourceLocaleObject = $page->getSourceLocale(); + $sourceLocaleObject = $page->getSourceLocale($frontendContext); $this->assertEquals( $expected, $sourceLocaleObject->Locale,