Skip to content

Commit

Permalink
Fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Apr 8, 2024
1 parent 9700b99 commit 589cd57
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/Extension/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/Model/RecordLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
21 changes: 5 additions & 16 deletions tests/php/Extension/LocaleInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -60,18 +49,18 @@ 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();
$page->Title = 'Page title';
$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,
Expand All @@ -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,
Expand Down

0 comments on commit 589cd57

Please sign in to comment.