Skip to content

Commit

Permalink
PR fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Apr 9, 2024
1 parent 033ac0f commit 680d87c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
25 changes: 14 additions & 11 deletions src/Extension/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions src/Extension/FluentVersionedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 12 additions & 2 deletions src/Extension/Traits/FluentBadgeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'];
Expand All @@ -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 {
Expand Down
19 changes: 11 additions & 8 deletions src/Model/RecordLocale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Extension/LocaleInheritanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 680d87c

Please sign in to comment.