Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes 16: ModelWithContent simplifications #6504

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Cms/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,27 @@ public function exists(): bool
return file_exists($this->root());
}

/**
* Compare too language objects
*/
public function is(self $language): bool
{
return $this->id() === $language->id();
}

/**
* Checks if this is language is the
* current language
*/
public function isCurrent(): bool
{
if ($this->isSingle() === true) {
return true;
}

return App::instance()->currentLanguage()->is($this);
}

/**
* Checks if this is the default language
* for the site.
Expand Down
28 changes: 3 additions & 25 deletions src/Cms/ModelWithContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,11 @@ public function content(string|null $languageCode = null): Content
{
// single language support
if ($this->kirby()->multilang() === false) {
if ($this->content instanceof Content) {
return $this->content;
}

// don't normalize field keys (already handled by the `Data` class)
return $this->content = new Content($this->readContent(), $this, false);
return $this->content ??= $this->version()->content('default');
}

// get the targeted language
$language = $this->kirby()->language($languageCode);

// stop if the language does not exist
if ($language === null) {
throw new InvalidArgumentException('Invalid language: ' . $languageCode);
}
$language = Language::ensure($languageCode);

// only fetch from cache for the current language
if ($languageCode === null && $this->content instanceof Content) {
Expand Down Expand Up @@ -735,19 +725,7 @@ public function version(VersionId|string|null $versionId = null): Version
*/
public function writeContent(array $data, string|null $languageCode = null): bool
{
$data = $this->contentFileData($data, $languageCode);

// update the default language, unless a specific language is passed
$languageCode ??= 'default';

try {
// we can only update if the version already exists
$this->version()->update($data, $languageCode);
} catch (NotFoundException) {
// otherwise create a new version
$this->version()->create($data, $languageCode);
}

$this->version()->save($data, $languageCode ?? 'current', true);
return true;
}
}
43 changes: 43 additions & 0 deletions tests/Cms/Languages/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,49 @@ public function testExists()
$this->assertTrue($language->exists());
}

/**
* @covers ::is
*/
public function testIs()
{
$en = new Language([
'code' => 'en'
]);

$de = new Language([
'code' => 'de'
]);

$this->assertTrue($en->is($en));
$this->assertFalse($en->is($de));
}

/**
* @covers ::isCurrent
*/
public function testIsCurrent()
{
$this->app = $this->app->clone([
'languages' => [
[
'code' => 'en',
'default' => true
],
[
'code' => 'de',
]
]
]);

$this->assertTrue($this->app->language('en')->isCurrent());
$this->assertFalse($this->app->language('de')->isCurrent());

$this->app->setCurrentLanguage('de');

$this->assertTrue($this->app->language('de')->isCurrent());
$this->assertFalse($this->app->language('en')->isCurrent());
}

/**
* @covers ::isDefault
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/Cms/Models/ModelWithContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kirby\Content\Version;
use Kirby\Content\VersionId;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Panel\Page as PanelPage;
use Kirby\Uuid\PageUuid;
use Kirby\Uuid\SiteUuid;
Expand Down Expand Up @@ -155,7 +156,7 @@ public function testContentForInvalidTranslation()
]
]);

$this->expectException(InvalidArgumentException::class);
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('Invalid language: fr');

$app->page('foo')->content('fr');
Expand Down