diff --git a/src/Extension/FluentExtension.php b/src/Extension/FluentExtension.php index 0426bea8..b984355b 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; @@ -965,18 +964,12 @@ protected function getRecordLocale() return Locale::getCurrentLocale(); } - /** * 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 case, - * but you can optionally specify CMS context as well - * Passing null will fall back to whatever context is currently in use in global fluent state - * - * @param bool|null $isFrontend * * @return Locale|null */ - public function getSourceLocale(?bool $isFrontend = true): ?Locale + public function getSourceLocale(): ?Locale { $currentLocale = FluentState::singleton()->getLocale(); @@ -988,7 +981,7 @@ public function getSourceLocale(?bool $isFrontend = true): ?Locale $owner = $this->owner; $localeInformation = $owner->LocaleInformation($currentLocale); - return $localeInformation->getSourceLocale($isFrontend); + return $localeInformation->getSourceLocale(); } /** @@ -1276,11 +1269,21 @@ public function updateLocalisationTabColumns(&$summaryColumns) $summaryColumns['Source'] = [ 'title' => 'Source', 'callback' => function (Locale $object) { - if (!$object->RecordLocale()) { + $localeInformation = $object->RecordLocale(); + + if (!$localeInformation) { return ''; } - $sourceLocale = $object->RecordLocale()->getSourceLocale(); + $sourceLocale = FluentState::singleton()->withState( + static function (FluentState $state) use ($localeInformation): ?Locale { + // We are currently in the CMS context, but we want to show to the context author + // what the data state is in the frontend context + $state->setIsFrontend(true); + + return $localeInformation->getSourceLocale(); + } + ); if ($sourceLocale) { return $sourceLocale->getLongTitle(); diff --git a/src/Extension/FluentVersionedExtension.php b/src/Extension/FluentVersionedExtension.php index 83ddc7df..00a5f80c 100644 --- a/src/Extension/FluentVersionedExtension.php +++ b/src/Extension/FluentVersionedExtension.php @@ -616,11 +616,21 @@ public function updateLocalisationTabColumns(&$summaryColumns) $summaryColumns['Source'] = [ 'title' => 'Source', 'callback' => function (Locale $object) { - if (!$object->RecordLocale()) { + $localeInformation = $object->RecordLocale(); + + if (!$localeInformation) { return ''; } - $sourceLocale = $object->RecordLocale()->getSourceLocale(); + $sourceLocale = FluentState::singleton()->withState( + static function (FluentState $state) use ($localeInformation): ?Locale { + // We are currently in the CMS context, but we want to show to the context author + // what the data state is in the frontend context + $state->setIsFrontend(true); + + return $localeInformation->getSourceLocale(); + } + ); if ($sourceLocale) { return $sourceLocale->getLongTitle(); diff --git a/src/Extension/Traits/FluentBadgeTrait.php b/src/Extension/Traits/FluentBadgeTrait.php index f47d7802..d4f3e7ee 100644 --- a/src/Extension/Traits/FluentBadgeTrait.php +++ b/src/Extension/Traits/FluentBadgeTrait.php @@ -9,6 +9,7 @@ use TractorCow\Fluent\Extension\FluentFilteredExtension; use TractorCow\Fluent\Model\Locale; use TractorCow\Fluent\Model\RecordLocale; +use TractorCow\Fluent\State\FluentState; trait FluentBadgeTrait { @@ -68,6 +69,15 @@ protected function generateBadgeHTML( $extraProperties = [] ) { $info = RecordLocale::create($record, $locale); + $sourceLocale = FluentState::singleton()->withState( + static function (FluentState $state) use ($info): ?Locale { + // We are currently in the CMS context, but we want to show to the context author + // what the data state is in the frontend context + $state->setIsFrontend(true); + + return $info->getSourceLocale(); + } + ); // Build new badge $badgeClasses = ['badge', 'fluent-badge']; @@ -81,14 +91,14 @@ protected function generateBadgeHTML( 'locale' => $locale->getTitle() ] ); - } elseif ($info->getSourceLocale()) { + } elseif ($sourceLocale) { // If object is inheriting content from another locale show the source $badgeClasses[] = 'fluent-badge--localised'; $tooltip = _t( __TRAIT__ . '.BadgeInherited', 'Inherited from {locale}', [ - 'locale' => $info->getSourceLocale()->getTitle() + 'locale' => $sourceLocale->getTitle() ] ); } else { diff --git a/src/Model/RecordLocale.php b/src/Model/RecordLocale.php index a66e7945..764d4893 100644 --- a/src/Model/RecordLocale.php +++ b/src/Model/RecordLocale.php @@ -276,7 +276,15 @@ public function IsPublished($inLocale = false): bool // If frontend publishing is not required for localisation, // we need to check if record is published in the source locale if (!$inLocale && $record->config()->get('frontend_publish_required') !== FluentExtension::INHERITANCE_MODE_EXACT) { - $sourceLocale = $this->getSourceLocale(); + $sourceLocale = FluentState::singleton()->withState( + function (FluentState $state): ?Locale { + // We are currently in the CMS context, but we want to show to the context author + // what the data state is in the frontend context + $state->setIsFrontend(true); + + return $this->getSourceLocale(); + } + ); if (!$sourceLocale) { // No source locale available @@ -360,21 +368,16 @@ 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 case, - * but you can optionally specify CMS context as well - * Passing null will fall back to whatever context is currently in use in global fluent state * - * @param bool|null $isFrontend * @return Locale|null */ - public function getSourceLocale(?bool $isFrontend = true): ?Locale + public function getSourceLocale(): ?Locale { - $isFrontend ??= FluentState::singleton()->getIsFrontend(); - /** @var DataObject|FluentExtension $record */ $record = $this->getOriginalRecord(); $config = $record->config(); + $isFrontend = 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 2b617887..a567e5c6 100644 --- a/tests/php/Extension/LocaleInheritanceTest.php +++ b/tests/php/Extension/LocaleInheritanceTest.php @@ -60,7 +60,7 @@ function (FluentState $state) use ($frontendContext, $locale, $expected): void { $page->write(); $localeInformation = $page->LocaleInformation($locale); - $sourceLocaleObject = $localeInformation->getSourceLocale($frontendContext); + $sourceLocaleObject = $localeInformation->getSourceLocale(); $sourceLocale = $sourceLocaleObject?->Locale; $this->assertEquals( $expected, @@ -79,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($frontendContext); + $sourceLocaleObject = $page->getSourceLocale(); $this->assertEquals( $expected, $sourceLocaleObject->Locale,