From b23828385b13dd2642ef65a52e516d2fe5e91050 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 5 Apr 2024 13:28:03 +1300 Subject: [PATCH] FIX: Source locale indicator correction. --- src/Extension/FluentExtension.php | 22 +++++++++++++------ .../FluentVersionedExtensionTest.php | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Extension/FluentExtension.php b/src/Extension/FluentExtension.php index ff4f6820..575c100a 100644 --- a/src/Extension/FluentExtension.php +++ b/src/Extension/FluentExtension.php @@ -24,7 +24,6 @@ use SilverStripe\ORM\Queries\SQLConditionGroup; use SilverStripe\ORM\Queries\SQLSelect; use SilverStripe\ORM\ValidationException; -use SilverStripe\Security\Permission; use SilverStripe\Versioned\Versioned; use SilverStripe\View\HTML; use TractorCow\Fluent\Extension\Traits\FluentObjectTrait; @@ -48,6 +47,7 @@ * * @template T of DataObject * @extends DataExtension + * @property DataObject|$this $owner */ class FluentExtension extends DataExtension { @@ -582,11 +582,11 @@ public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) // Apply substitutions $localisedPredicate = str_replace($conditionSearch, $conditionReplace, $predicate); - + if (empty($localisedPredicate)) { continue; } - + $where[$index] = [ $localisedPredicate => $parameters ]; @@ -965,19 +965,27 @@ protected function getRecordLocale() return Locale::getCurrentLocale(); } - /** * Returns the source locale that will display the content for this record * * @return Locale|null */ - public function getSourceLocale() + public function getSourceLocale(): ?Locale { - $sourceLocale = $this->owner->getField('SourceLocale'); + $owner = $this->owner; + $currentLocale = FluentState::singleton()->getLocale(); + + // We do not have a locale set, so we can't determine a source + if (!$currentLocale) { + return null; + } + + $localeInformation = $owner->LocaleInformation($currentLocale); + $sourceLocale = $localeInformation->getSourceLocale(); if ($sourceLocale) { return Locale::getByLocale($sourceLocale); } - return Locale::getDefault(); + return null; } /** diff --git a/tests/php/Extension/FluentVersionedExtensionTest.php b/tests/php/Extension/FluentVersionedExtensionTest.php index 44e97905..7ab3986e 100644 --- a/tests/php/Extension/FluentVersionedExtensionTest.php +++ b/tests/php/Extension/FluentVersionedExtensionTest.php @@ -102,7 +102,7 @@ public function testSourceLocaleIsCurrentWhenPageExistsInIt() ->setIsDomainMode(false); // Read from the locale that the page exists in already - /** @var Page $page */ + /** @var Page|FluentSiteTreeExtension $page */ $page = $this->objFromFixture(Page::class, 'home'); $this->assertEquals('en_NZ', $page->getSourceLocale()->Locale);